Search code examples
androidktorktor-client

Why upload file is not completed ink tor client MultiPartFormDataContent?


I chunked a file to byteArray and I want to upload each chunk to separate request with ktor client MultiPartFormDataContent for uploading some chunk files but after reading a ByteArrays, the file is not uploaded.

Here is my request:

 override suspend fun uploadChunks(
        token: String,
        file: ByteArray,
        fileId: Long,
        chunkIndex: String,
        fileType: Int,
        fileName: String,
    ): NetworkUploadChunks? = try {
        
        KtorModule.provideKtor.use {
            it.post(".../uploadChunks") {
                headers {
                    bearerAuth(token)
                }
                setBody(MultiPartFormDataContent(
                    formData {
                        append("FileId", fileId)
                        append("FileChunkId", chunkIndex)
                        append("FileType", fileType)
                        append(
                            "FileChunk", file,
                            Headers.build {
                                append(
                                    HttpHeaders.ContentType, when (fileType) {
                                        0 -> ContentType.Video.MP4
                                        else -> {
                                            ContentType.Text.CSV
                                        }
                                    }
                                )
                                append(
                                    HttpHeaders.ContentDisposition,
                                    "filename=\"${fileName}\""
                                )
                            },
                        )
                    },
                    boundary = "WebAppBoundary"
                ))

                onUpload { bytesSentTotal, contentLength ->
                    println("Sent $bytesSentTotal bytes from $contentLength")
                }

            }.body()
        }
    } catch (cause: ClientRequestException) {
        Log.d("clientRequestException:", cause.message)
        null
    } catch (cause: ProtocolException) {
        Log.d("ProtocolException:", cause.message.toString())
        null
    } catch (cause: CancellationException) {
        Log.d("CancellationException:", cause.message.toString())
        null
    }

it's logcat for uploading state:

2023-01-08 13:55:46.256 18174-18376/de.artifici... V/Logger Ktor =>: REQUEST: http://gizo.ml/api/v1/Trip/uploadChunks
    METHOD: HttpMethod(value=POST)
    COMMON HEADERS
    -> Accept: application/json; application/json
    -> Accept-Charset: UTF-8
    -> Authorization: Bearer eyJhbGciOiJ...
    CONTENT HEADERS
    -> Content-Length: 1299875
    -> Content-Type: multipart/form-data; boundary=WebAppBoundary
    BODY Content-Type: multipart/form-data; boundary=WebAppBoundary
    BODY START
    [request body omitted]
    BODY END
2023-01-08 14:00:08.746 18174-18255/de.artificient.gizo V/Logger Ktor =>: REQUEST http://....ml/api/v1/Trip/uploadChunks failed with exception: java.io.IOException: unexpected end of stream on http://...ml/...

enter image description here

as you can see, all byte arrays values are read but it's not uploaded


Solution

  • After researching problem I found solution, maybe it will be helpful.

    Change LogLevel from ALL to other type (INFO, BODY, HEADERS or NONE)

    HttpClient(engine) {
    
        if (configuration.isHttpLoggingEnabled) {
            install(Logging) {
                logger = Logger.SIMPLE
                level = LogLevel.INFO // changed from LogLevel.ALL
            }
        }
     }