Skip to content

Instantly share code, notes, and snippets.

@danielgomezrico
Last active May 4, 2017 20:27
Show Gist options
  • Save danielgomezrico/4586a7656acfb779bb489538f1310876 to your computer and use it in GitHub Desktop.
Save danielgomezrico/4586a7656acfb779bb489538f1310876 to your computer and use it in GitHub Desktop.
Android / Retrofit 2 - Upload file
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
}
@Multipart
@PUT("/api/files")
fun uploadFile(@Part file: MultipartBody.Part): Observable<Response<Any>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment