1

I am trying to upload files to the server using Retrofit 2 with a PUT request.

This is how I do it:

@Multipart
@PUT
Call<Void> uploadFile(@Url String presignedUrl, @Part MultipartBody.Part file, @Part("file") RequestBody name);

Upload text file:

FileWriter writer = new FileWriter(m_capturedFile);
                        writer.append(m_weakActivity.get().m_addContentNoteEdit.getText().toString());
                        writer.flush();
                        writer.close();

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), m_capturedFile);
                        MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", m_capturedFile.getName(), requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), m_capturedFile.getName());

response = FileUploadApiWrapper.getService().uploadFile(predefinedUrl, fileToUpload,filename).execute();

The issue is, while I create and save the file to the Android file system, the file is created properly. However, when I upload it the file gets appended with some headers giving a file as follows:-

--292211e9-3b63-4b3e-8f49-bc75605656ac
Content-Disposition: form-data; name="file"; filename="TXT15782900294806741473599448458222.txt"
Content-Type: */*
Content-Length: 11

Sample text for testing
--292211e9-3b63-4b3e-8f49-bc75605656ac
Content-Disposition: form-data; name="file"
Content-Transfer-Encoding: binary
Content-Type: text/plain; charset=utf-8
Content-Length: 39

TXT15782900294806741473599448458222.txt
--292211e9-3b63-4b3e-8f49-bc75605656ac--

How can I solve this issue where the correct file content is uploaded?
Any help is appreciated.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Pooja Gaonkar
  • 1,546
  • 17
  • 27

1 Answers1

0

check by passing multipart/form-data

RequestBody requestBody =
 RequestBody.create(MediaType.parse("multipart/form-data"), m_capturedFile);
                        MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", m_capturedFile.getName(), requestBody);

and don't pass 3rd argument because file name is already in requestBody

Sejpal Pavan
  • 130
  • 10