How to post multiple image files using ion library?

半腔热情 提交于 2019-12-23 02:33:27

问题


I am using this code to upload single image file on server.

But i need to upload multiple 'n' number of files at once

Ion.with(MainActivity.this)
                .load(Constant.UPLOAD_IMG)
                .setMultipartFile("UploadForm[imageFiles]", imgFile.getName(), imgFile)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                    }
                });

I tried to get MultipartBodyBuilder separately.

 MultipartBodyBuilder body = Ion.with(MainActivity.this)
                .load(Constant.UPLOAD_IMG);

 body.setMultipartFile("UploadForm[imageFiles]", imgFile.getName(), imgFile);

回答1:


Use addMultipartParts to add a list of FilePart or StringParts.

https://github.com/koush/ion/blob/master/ion/src/com/koushikdutta/ion/builder/MultipartBodyBuilder.java#L55




回答2:


Here is snippet code to use addMultipartParts

List < Part > files = new ArrayList();
for (int i = 0; i < imageFiles; i++) {
  files.add(new FilePart("UploadForm[" + i + "]", new File(imgFile.get(i).getName())));
}

Ion.with(MainActivity.this)
  .load(Constant.UPLOAD_IMG)
  .addMultipartParts(files)
  .asJsonObject()
  .setCallback(new FutureCallback < JsonObject > () {
    @Override
    public void onCompleted(Exception e, JsonObject result) {}
  });


来源:https://stackoverflow.com/questions/31488511/how-to-post-multiple-image-files-using-ion-library

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