Created
February 25, 2020 20:21
-
-
Save lordcodes/189b2d886f8c549f0814110a9f0f82ba to your computer and use it in GitHub Desktop.
Code for the article: "Uploading a file with progress in Kotlin"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private fun createUploadRequest( | |
| filename: String, | |
| file: File, | |
| mimeType: String, | |
| progressEmitter: PublishSubject<Double> | |
| ): Single<AttachmentUploadedRemoteDto> { | |
| val requestBody = createUploadRequestBody(file, mimeType, progressEmitter) | |
| return remoteApi.attachFile( | |
| filename = filename.toPlainTextBody(), | |
| mimeType = mimeType.toPlainTextBody(), | |
| fileSize = file.length().toString().toPlainTextBody(), | |
| filePart = MultipartBody.Part.createFormData( | |
| name = "files[]", | |
| filename = filename, | |
| body = requestBody | |
| ) | |
| ) | |
| } | |
| private fun String.toPlainTextBody() = toRequestBody("text/plain".toMediaType()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment