How to post multiple image files using ion library

2019-08-10 07:08发布

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);

2条回答
beautiful°
2楼-- · 2019-08-10 07:13
啃猪蹄的小仙女
3楼-- · 2019-08-10 07:35

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) {}
  });
查看更多
登录 后发表回答