Retrofit: Feature request: multiple file uploading via @Part List<MultipartBody.Part> or @PartMap<String, MultipartBody.Part>

Created on 29 Mar 2016  路  5Comments  路  Source: square/retrofit

[Feature Request]
Since 2.0.0, setting a filename for an upload file is resolved by OkHttp's MultipartBody.Part,
but the support limited to one file per param via (@Part MultipartBody.Part part).

Multiple file uploading via @PartMap or @Part List<> is still unsupported.

There're two approaches to achieve that.

1) Support @Part List<MultipartBody.Part> as method parameter
When parsing @Part in ServiceMethod.parseParameterAnnotation, allow the parameterizedType of Iterable to be MultipartBody.Part and allow each part setting its OWN headers, instead of setting a common header for all parts.

2) Support @PartMap Map<String, MultipartBody.Part> as method parameter
When parsing @PartMap in ServiceMethod.parseParameterAnnotation, allow the parameterizedType of Iterable to be MultipartBody.Part.
I noticed that, in the code block parsing @PartMap, an exception will be thrown when parameterizedType is detected to be MultipartBody.Part.
The exception message is "@PartMap values cannot be MultipartBody.Part. Use @Part List<Part> or a different value type instead."
Sadly @Part List<Part> is also not supported yet. I supposed it should be @Part List<MultipartBody.Part>.

I prefer to the first approach, because I sometimes need to upload multiple files with same field name (but possibly different filenames), which can't be done with @PartMap unless multimap type support is added.

Thanks.

Most helpful comment

@Kolyall
What is PHP Code for get that "@Part List parts"?

All 5 comments

We meant to include list and array support. Will add. The map makes no sense since the key is redundant with the headers inside the Part.

Hello,
What is the current solution of uploading dynamic amount of files? I cannot seem to find any. Thx.

@anetefr
Example:

List<MultipartBody.Part> parts= new ArrayList();
for (int i = 0; i<5;i++){
 File file = new File(path+i);
part = MultipartBody.Part.createFormData("file"+i, file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
parts.add(part);
}
uploadFileObservable(url/*yourUrl*/,parts).subscribe(action/*anyAction*/);

  @Multipart
    @POST
    Observable<Void> uploadFilesObservable(@Url String url, @Part List<MultipartBody.Part> parts);

@Kolyall
What is PHP Code for get that "@Part List parts"?

@Kolyall Could you please post the full code for the benefit of us newbies. I am using an adapter and it cannot resolve the symbol path and part. Here is my code:

FilesUploadService filesUploadService = mApiService.RootService(FilesUploadService.class, EndPoints.BACKEND_BASE_URL); Call<FilesResponse> filesResponseCall; int messageId = messagesModel.getId();

        `List<MultipartBody.Part> parts= new ArrayList();
        for (int i = 0; i<5;i++){
            File file = new File(path+i);
            part = MultipartBody.Part.createFormData("file"+i, file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
            parts.add(part);
        }

        filesResponseCall = filesUploadService.uploadMessageImages(List<Uri> parts);

`
Any help will be highly appreaciated.

Was this page helpful?
0 / 5 - 0 ratings