【问题标题】:Upload bitmap file to server using volley and codeigniter使用 volley 和 codeigniter 将位图文件上传到服务器
【发布时间】:2018-03-29 05:22:06
【问题描述】:

我正在使用 volley 将我的位图文件上传到服务器。在服务器端,我正在使用 codeigniter 框架来完成这项工作,但是当我尝试上传文件时,我得到了来自服务器的响应,

“您没有选择要上传的文件”

这里是代码

凌空抽射

 public void uploadImage(final Context context, final Bitmap bitmap) {
        String url = "https://zenosama1111.000webhostapp.com/Upload/do_upload";
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                params.put("thumbnail", bitmapToString(bitmap));
                return params;
            }
        };
        requestQueue.add(request);
    }

服务器端

 public function do_upload()
        {
                $config['upload_path']          = './thumbnails/';
                $config['allowed_types']        = 'gif|jpg|png|jpeg';
                $config['max_size']             = 100;
                $config['max_width']            = 160;
                $config['max_height']           = 100;

                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('thumbnail'))
                {
                        echo false;
                }
                else
                {
                      echo true;
                }
                 $error = array('error' => $this->upload->display_errors());
                 echo json_encode($error);
        }

其他教程使用纯 php 代码上传文件,但不是在 codeigniter.. 有人知道我该如何解决这个问题?

【问题讨论】:

  • 如果你使用位图上传图片 $this->load->library('upload', $config);服务器端问题“上传”在此行中未使用使用文件路径并替换“上传”
  • 我对codeigniter上传库很熟悉,但对android不太熟悉。我看到bitmapToString 这一行让我相信您应该在$_POST['thumbnail'] 字段上使用文件放置内容。 CI 期望thumbnail 是来自$_FILES 字段的图像,其中thumbnail 是字段的名称。如果你print_r($_FILES) 我敢打赌什么都没有。
  • @Alex 我试过这段代码来测试$this-&gt;upload-&gt;do_upload($this-&gt;input-&gt;post('thumbnail')) 然后响应是一个只有计算机才能读取的长字符串。也许我应该做纯 php 而不是使用 CI 库来完成这项任务。

标签: android codeigniter android-volley


【解决方案1】:

这个$this-&gt;upload-&gt;do_upload($this-&gt;input-&gt;post('thumbnail'))$this-&gt;upload-&gt;do_upload('thumbnail') 不起作用。

do_upload使用来自$_FILES 数组(不同于后数组)的项目,并且do_upload($param) 的参数必须是文件输入的字段名。

例如&lt;input name="userfile" type="file"&gt; => do_upload('userfile')

它不打算按照您使用它的方式使用,因为您的项目位于 post 数组中。因此,file_put_contents($filename, $this-&gt;input-&gt;post('thumbnail')); 可能会满足您的需求。

如果您可以让项目位于 $_FILES 数组中,那么您可能有机会使用 CI 上传库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2016-08-21
    • 2017-07-21
    相关资源
    最近更新 更多