How to upload multiple files with AsyncHttpClient Android

拈花ヽ惹草 提交于 2019-12-04 04:03:38

You can pass a file array as value for the files key. In order to do that, follow the code below:

File[] myFiles = { new File("pic.jpg"), new File("pic1.jpg") }; 
RequestParams params = new RequestParams();
try {
    params.put("profile_picture[]", myFiles);
} catch(FileNotFoundException e) {

}

Aditionally, if you want a dynamic array, you can use an ArrayList and convert to File[] type with the method .toArray()

ArrayList<File> fileArrayList = new ArrayList<>();

//...add File objects to fileArrayList

File[] files = new File[fileArrayList.size()];  
fileArrayList.toArray(files);

Hope this help. =D

Rafael Sanches

Create the SimpleMultipartEntity object and call the addPart for each file that you want to upload.

Devraj Jha
 File[] files = lst.toArray(new File[lst.size()]);

    try {
        params.put("media[]", files);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
mcamocci

You should pass all your files as parameter on params. For example:

params.put("file_one", myFiles1);
params.put("file_two", myFiles2)
xurong

You should use this code:

public static void fileUpLoad(String url,File file,AsyncHttpResponseHandler asyncHttpResponseHandler){
    RequestParams requestParams=new RequestParams();

    try{
        requestParams.put("profile_picture", file,"application/octet-stream");
        client.post(url, requestParams, new AsyncHttpResponseHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!