Zenzen
Zenzen

Reputation: 61

Retrofit upload any file

I am trying to upload a file , this way i am doing.

@POST("News/Article/Add")
        Call<User> addNews(@Header("api_key") String auth,
                            @Query(encoded = true, value = "title") String title,
                            @Body RequestBody file
        );
File file = new File(uri1.getPath());
                               RequestBody file1 = RequestBody.create(MediaType.parse("file/*"), file);
Call call = App.addNews(AddNewsActivity.this).addNews(apiKey,inputTitle.getText().toString(), file1)

Am using compile 'com.squareup.retrofit2:retrofit:2.1.0'

First mistake am noticing, I need to send the requestbody as "contentFile". As u can see below, the one below works but only for images.I dont want to specify any format ( like png).Just upload the file as is.

RequestBody requestBody = new MultipartBody.Builder()
>                                 .setType(MultipartBody.FORM)
>                                 .addFormDataPart("contentFile", UUID.randomUUID() + ".png",
>                                         RequestBody.create(MEDIA_TYPE_PNG, f100)).build();

Upvotes: 0

Views: 399

Answers (1)

John
John

Reputation: 1314

You can assign media type dynamically:

 MediaType.parse(getContentResolver().getType(fileUri))

Upvotes: 0

Related Questions