https://kotlinlang.org logo
#ktor
Title
# ktor
g

galex

02/14/2019, 6:02 AM
When uploading an base64 encoded image with ktor-core (common sourceset) when the image is bigger than around 200k I get the following error:
Copy code
IllegalArgumentException {message_8yp7un$_0: "should should be greater than write space + end ga… = 9, writeRemaining = 0, endGap = 8, rem+gap = 8", cause_th0jdv$_0: null, name: "IllegalArgumentException", stack: "IllegalArgumentException: should should be greater…8td$_0 (<http://localhost:9000/bundle.js:31435:19>)"}
👀 1
My base64 file represented by a string is bigger than 1MB, I guess that’s the big deal here?
I should probably upload the bytes of that file, not its representation from FileReader() in js
r

Riccardo Montagnin

02/14/2019, 6:59 AM
I think the best way to upload images would be using the
form-part
body request type. With that you no longer have to process the file and have no limit on its dimension
g

galex

02/14/2019, 11:32 AM
So you wouldn't use multi part?
r

Riccardo Montagnin

02/14/2019, 1:30 PM
I would use it! I send all my files using
Content-Type: multipart/form-data
and had no problem about their sizes
g

galex

02/14/2019, 6:49 PM
Do you have an example of form using this with ktor-client?
r

Riccardo Montagnin

02/14/2019, 8:11 PM
There's an example inside the official documentation: https://ktor.io/clients/http-client/calls/requests.html#multipart-form-data https://ktor.io/clients/http-client/calls/requests.html#submit-form
Copy code
val data: List<PartData> = formData {
    // Can append: String, Number, ByteArray and Input.
    append("hello", "world")
    append("number", 10)
    append("ba", byteArrayOf(1, 2, 3, 4))
    append("input", inputStream.asInput())
    // Allow to set headers to the part:
    append("hello", "world", headersOf("X-My-Header" to "MyValue"))
}
g

galex

02/15/2019, 4:29 AM
Ah nice I totally missed that
I only miss now how to go from the inputFile type to an inputStream or even a byteArray, who cares if we sent it in multi part correctly!
I continue to hit a buffer limit in IoBuffer in kotlin-io I guess
r

Riccardo Montagnin

02/15/2019, 10:11 AM
Could you post your code here?
g

galex

02/19/2019, 9:58 AM
@Riccardo Montagnin I posted a but to ktor.io and it seems they have already a fix for it planned for 1.1.3 https://github.com/ktorio/ktor/issues/956
3 Views