Hi, When I was implementing the file upload functi...
# ktor
a
Hi, When I was implementing the file upload functionality using ktor. While I can upload the file without upload progress, when i adding the
onUpload { }
throwing an exception
ProtocolException("unexpected end of stream")
. I've searched for a solution but haven't found any, even though this issue was reported long time back. @Aleksei Tirman [JB] Did you have any idea about this?
Copy code
val client = HttpClient(OkHttp)

val response: HttpResponse = <http://client.post|client.post>("presigned_url") {
    setBody(MultiPartFormDataContent(
        formData {
            append("description", "PDF")
            append("image", File("dummy.pdf").readBytes(), Headers.build {
                append(HttpHeaders.ContentType, "application/pdf")
                append(HttpHeaders.ContentDisposition, "filename=\"dummy.pdf\"")
            })
        },
        boundary = "WebAppBoundary"
    )
    )
    onUpload { bytesSentTotal, contentLength ->
        println("Sent $bytesSentTotal bytes from $contentLength")
    }
}
a
I cannot reproduce the exception by sending the request to https://httpbin.org/post using your sample code. Do you have any plugins installed? What URL should the client make a request to reproduce the exception?
a
I'm using S3 presigned url, to upload file. I followed this section Issue only occurs when I added
Copy code
onUpload { bytesSentTotal, contentLength -> }
a
Can you share that URL?
I can only recommend trying another client engine at the moment.
👍 1
d
Hello @Aman try the below suspend fun uploadDocuments( body: ByteArray, contentType: String, fileName: String, onProgressUpdate: (UploadProgress) -> Unit ): UploadResponse { return ErrorHandler { val client2 = HttpClient(CIO) { install(Logging) { level = LogLevel.ALL } } client2.post(NetworkConstants.DocumentsUpload.route) { header(HttpHeaders.Authorization, “Bearer $token”) url { encodedParameters.append(“documentRefId”, documentRefId) encodedParameters.append(“type”, “PAYSLIP”) encodedParameters.append(“description”, “Uploaded”) } setBody( MultiPartFormDataContent( formData { append(“description”, “Ktor logo”) append(“file”, body, Headers.build { append(HttpHeaders.ContentType, contentType) append(HttpHeaders.ContentDisposition, “filename=\“$fileName\“”) }) }, boundary = “WebAppBoundary” ) ) onUpload { bytesSentTotal, contentLength -> onProgressUpdate(UploadProgress(bytesSentTotal, contentLength)) } } } }