Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created February 25, 2020 20:20
Show Gist options
  • Save lordcodes/217e34ec571e3a3b2bf8d1305c6fa0bc to your computer and use it in GitHub Desktop.
Save lordcodes/217e34ec571e3a3b2bf8d1305c6fa0bc to your computer and use it in GitHub Desktop.
Code for the article: "Uploading a file with progress in Kotlin"
private fun createUploadRequestBody(
file: File,
mimeType: String,
progressEmitter: PublishSubject<Double>
): RequestBody {
val fileRequestBody = file.asRequestBody(mimeType.toMediaType())
return CountingRequestBody(fileRequestBody) { bytesWritten, contentLength ->
val progress = 1.0 * bytesWritten / contentLength
progressEmitter.onNext(progress)
if (progress >= 1.0) {
progressEmitter.onComplete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment