代码之家  ›  专栏  ›  技术社区  ›  basaya

使用volley和codeigniter将位图文件上载到服务器

  •  0
  • basaya  · 技术社区  · 6 年前

    我正在使用截图将位图文件上载到服务器。在服务器端,我使用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中。。 有人知道我如何解决这个问题吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Alex    6 年前

    $this->upload->do_upload($this->input->post('thumbnail')) $this->upload->do_upload('thumbnail') 行不通。

    do_upload 只有 使用中的项目 $_FILES 数组(与后数组不同)和的参数 do_upload($param) 必须是文件输入的字段名。

    e、 g。 <input name="userfile" type="file"> => do_upload('userfile')

    它的使用方式与您使用它的方式不同,因为您的项目位于post数组中。因此可能 file_put_contents($filename, $this->input->post('thumbnail')); 可以满足您的需要。

    如果你能在 $\u文件 阵列,则您可能有机会使用CI上载库。