Retrofit Uploading multiple images to a single key

前端 未结 4 711
孤独总比滥情好
孤独总比滥情好 2020-11-28 05:10

I am using Retrofit to upload images to my server. Here I need to upload multiple images for a single key. I have tried with Postman web client it is working well. Here is a

4条回答
  •  一生所求
    2020-11-28 05:44

    Best solution ever I have tried

    ApiInterface:

    @Multipart
        @POST("person/img")
        Call upImageMany(@Part List file);
    

    Activity:

    List parts = new ArrayList<>();
    
    for (int i=0; i < upFileList.size(); i++){
        parts.add(prepareFilePart("my_file["+i+"]", upFileList.get(i)));
    }
    
    private MultipartBody.Part prepareFilePart(String partName, Uri fileUri){
    
            File file = new File(getPath(fileUri));
    
            RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
    
            return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
        }
    

提交回复
热议问题