问题
I want to upload few files from my android code so that it is available in the $_FILES array.
The server is looking for the files as an array. The php code in the server looks like
for ($i=0;$i<$NumFiles;$i++){
...
$filename = $_FILES["fileset"]["name"][$i];
$filetype = $_FILES["fileset"]["type"][$i];
$file = curl_image_create($filePath,$filetype,$filename);
...
}
I have the files loaded in a File[] (fileSet) for my android code
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(this.apiURL);
MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();
StringBody numFilesBody = new StringBody("" + fileSet.length, ContentType.TEXT_PLAIN);
mpEntity.addPart("NumFiles", numFilesBody);
for (int i = 0; i < fileSet.length; i++) {
//What should I be doing here to get the $_FILES set up correctly
}
request.setEntity(mpEntity.build());
HttpResponse response = client.execute(request);
I need to set up the HTT PRequest so that the server can read the files in the $_Files as an array.
回答1:
File files [] = .....
Then add them in the for loop as:
mpEntity.addPart( "fileset[" + i + "]", new FileBody(files[i]) );
来源:https://stackoverflow.com/questions/31553036/uploading-file-array-from-android-to-files