Upload a picture from an Android java application to Codeigniter

me and my friend have a problem when it comes to uploading a picture from an android application to the Codeigniter framework in php. The problem arises (we think) because it's not an image file and therefore can't be used for image operations. One big notice is that the image upload works when tested with a html-page. And all the other data (like email) are provided and works in the android application. The only thing that do not work is the file upload. But it does work with another php file, but the question I have is how do I take the last php script that work and transform it into codeigniter? I want to use the framework for this, but so far we haven't been able to. Here is the javacode for the upload -> HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost("http://url.com/controller-name"); try { CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener() { @Override public void transferred(long num) { publishProgress((int) ((num / (float) totalSize) * 100)); } }); // We use FileBody to transfer an image multipartContent.addPart("userfile", new FileBody(new File(filename))); totalSize = multipartContent.getContentLength(); multipartContent.addPart("email", new StringBody("email@email.com")); multipartContent.addPart("submit", new StringBody("upload")); // Send it httpPost.setEntity(multipartContent); HttpResponse response = httpClient.execute(httpPost, httpContext); String serverResponse = EntityUtils.toString(response.getEntity()); Log.i("SERVER", "Response: " + response.toString()); return serverResponse; } catch (Exception e) { System.out.println(e); } The codeigniter method for uploading: function do_upload() { $image_name = time() . $this->get_random_string(); $config = array( 'file_name' => $image_name, 'allowed_types' => 'jpg|jpeg|gif|png', 'upload_path' => $this->gallery_path ); $this->load->library('upload', $config); $this->upload->do_upload(); $image_data = $this->upload->data(); $config = array( 'source_image' => $image_data['full_path'], 'new_image' => $this->gallery_path . '/thumbs', 'maintain_ratio' => false, 'width' => 300, 'height' => 300 ); $this->load->library('image_lib', $config); $this->image_lib->resize(); echo $this->image_lib->display_errors(); $image_name = $image_name . $image_data['file_ext']; //Insert into the database $data = array( 'image_name' => $image_name, 'upload_email' => $this->input->post('email'), 'ip' => $this->input->ip_address() ); // Insert the new data of the image! $insert = $this->db->insert('table', $data); $short_url = $this->alphaID($this->db->insert_id()); return $short_url; } The older php script that works, notice is that the commented code does not work. Since it would probably give the same effect as CI does. 0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; if (file_exists("images/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $_FILES["file"]["name"]); echo "Stored in: " . "images/" . $_FILES["file"]["name"]; } } /*} else { echo "Invalid file"; }*/ ?> Please help us modify either the CI-function or the java code so we can upload our beloved images. Thanks for your advice and better wisdom!
If you add echo $this->upload->display_errors();, the application will output "No file selected" as if it didn't have any file to begin with! :(

以上就是Upload a picture from an Android java application to Codeigniter的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏