简体   繁体   English

使用Volley和Codeigniter将位图文件上传到服务器

[英]Upload bitmap file to server using volley and codeigniter

I am using volley to upload my bitmap file to the server. 我正在使用凌空将我的位图文件上传到服务器。 In server side, i'm using codeigniter framework to do that job but i've got response from the server when I trying to upload the file that says, 在服务器端,我正在使用codeigniter框架来完成这项工作,但是当我尝试上传文件时说,我已经收到服务器的响应,

"you did not select a file to upload" “您没有选择要上传的文件”

here is the code 这是代码

Volley 凌空抽射

 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);
    }

server side 服务器端

 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);
        }

other tutorials are using pure php code to upload a file but not in codeigniter.. somebody know how can i fix this problem? 其他教程正在使用纯PHP代码上传文件,但未在codeigniter中..有人知道如何解决此问题?

This $this->upload->do_upload($this->input->post('thumbnail')) or $this->upload->do_upload('thumbnail') won't work. $this->upload->do_upload($this->input->post('thumbnail'))$this->upload->do_upload('thumbnail')无效。

do_upload will only work with an item from the $_FILES array (different than post array) and the parameter of do_upload($param) must be the fieldname of the file input. do_upload 适用于$_FILES数组中的项目(与post数组不同),并且do_upload($param)必须是文件输入的字段名。

eg <input name="userfile" type="file"> => do_upload('userfile') 例如<input name="userfile" type="file"> => do_upload('userfile')

It isn't intended to be used the way you are using it as your item is in the post array. 它不打算以您使用它的方式使用,因为您的项目在post数组中。 Hence maybe a file_put_contents($filename, $this->input->post('thumbnail')); 因此,也许是file_put_contents($filename, $this->input->post('thumbnail')); would work for your needs. 会满足您的需求。

If you could get the item to be in the $_FILES array then you might have a chance of using the CI upload library. 如果您可以将该项目保存在$_FILES数组中,则可以使用CI上传库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM