[英]Upload multiple image file to php server from android app
I am new to android programming. 我是android编程的新手。 I have been trying to upload multiple images from my app to server using the following code. 我一直在尝试使用以下代码将多个图像从我的应用程序上传到服务器。 Here image[] array is posted to server with other data like username, password,etc which is in namevaluepair. 这里image []数组与namevaluepair中的用户名,密码等其他数据一起发布到服务器。
Java code: Java代码:
public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
for (int index = 0; index < nameValuePairs.size(); index++) {
if (nameValuePairs.get(index).getName()
.equalsIgnoreCase("image[]")) {
// If the key equals to "image[]", we use FileBody to transfer
// the data
Log.d("key", "image");
Log.d("image path", nameValuePairs.get(index).getName());
entity.addPart(nameValuePairs.get(index).getName(),
new FileBody(new File(nameValuePairs.get(index)
.getValue())));
} else {
// Normal string data
Log.d("tag", nameValuePairs.get(index).getName());
// Log.d("tag", nameValuePairs.get(index).getValue());
entity.addPart(
nameValuePairs.get(index).getName(),
new StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity enttiy = response.getEntity();
Log.d("server response to post data", EntityUtils.toString(enttiy));
} catch (IOException e) {
e.printStackTrace();
}
}
Php code (Working version): PHP代码(工作版):
<?php
$target_path = "user_uploaded_photos/";
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
$username = $_POST['username'];
print_r($fileData);
$date = date('Y_m_d_H_i_s');
$newfilename = $username.$i.$date.".".$fileData['extension'];
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path."/".$newfilename))
{
echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />";
}
else
{
echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
} $location = $_POST['location'];
//other fields and database operations ommitted
My problem is with the cakephp website we are using. 我的问题在于我们正在使用的cakephp网站。 For cake php I use name value pairs as follows: 对于cake php,我使用名称值对,如下所示:
nameValuePair
.add(new BasicNameValuePair("UserDatum[user_id]", "2"));
Sending string values like this works for me. 像这样发送字符串值对我有用。 But when I declare images[] array images are uploaded but cannot be accessed from $_FILES. 但是当我声明images []数组图像被上传但无法从$ _FILES访问时。 I have declared images[] array as follows: 我已声明images []数组如下:
for (String s : imagePath) {
nameValuePair.add(new BasicNameValuePair("UserDatum[images][]",
s));
Log.d("image-path", s);
}
Here imagePath is arrayList of string containing path of images. 这里imagePath是包含图像路径的字符串的arrayList。 Is declaring "UserDatum[images][]" in app the correct way to do it? 在应用程序中以正确的方式声明“UserDatum [images] []”吗? It works with the posted php but in cake php only string values are posted and not images. 它适用于发布的php,但在蛋糕php只发布字符串值而不是图像。 I have tried other librarys like ion . 我尝试过像ion这样的其他图书馆。 But I am only able to upload one photo without using array structure. 但我只能在不使用数组结构的情况下上传一张照片。 Please comment or point me to the right direction for posting multiple images and strings in a single post. 请评论或指出我在一个帖子中发布多个图像和字符串的正确方向。
Whenever I post image data from an app, I use Base64 encoded image (see: Android post Base64 String to PHP ) 每当我从应用程序发布图像数据时,我都会使用Base64编码的图像(请参阅: Android将Base64字符串发布到PHP )
That might give you a better route. 这可能会给你一个更好的路线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.