Uploading file array from android to $_FILES

扶醉桌前 提交于 2019-12-01 07:07:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!