caipivara
5/4/2017 - 8:26 PM

Android / Retrofit 2 - Upload file

Android / Retrofit 2 - Upload file

@Multipart
@PUT("/api/files")
fun uploadFile(@Part file: MultipartBody.Part): Observable<Response<Any>>
fun uploadFile(user: User, avatarPath: String, mimeType: String): Observable<Answer<Any>> {
  val avatarFile = File(avatarPath)
  val requestFile = RequestBody.create(MediaType.parse(mimeType.toLowerCase()), avatarFile)
  val body = MultipartBody.Part.createFormData("file", avatarFile.name, requestFile)

  return service.uploadFile(user.id, body) // Service is an instance created by retrofit with Service.kt
}