How to post multiple image files using ion library?
Asked Answered
H

2

1

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);
Hydrolyze answered 18/7, 2015 at 6:32 Comment(3)
Did you try with just adding more with .setMultipartFile() ?Grosmark
thanks @Grosmark , but i need to post ' n ' number of files dynamically .Hydrolyze
then just wrap the call in a loop?Premonitory
B
3

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

Bilinear answered 19/7, 2015 at 19:9 Comment(0)
D
3

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) {}
  });
Deviate answered 7/12, 2015 at 3:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.