With Ktor Server how is a binary file read from th...
# ktor
n
With Ktor Server how is a binary file read from the request body?
t
Copy code
val bytes = ByteArray(length)
call.receiveChannel().readFully(bytes, 0, length)
This reads everything into a byte array which is fine if your upload limit is small. You can use the channel to read chunks if the upload is bigger. Taken this example from: https://github.com/spxbhuhb/zakadabar-stack/blob/master/lib/blobs/src/jvmMain/kotlin/zakadabar/lib/blobs/business/BlobBlBase.kt
👍 1
n
@Aleksei Tirman [JB] - The Ktor Server documentation is missing a section on reading binary data from a HTTP request body.
I found this code example by accident which covers reading the binary data (in chucks) from a HTTP response body 😄: https://github.com/Kotlin/kotlinx-io/issues/41#issuecomment-469137967 Luckily the code example can be adapted to work with an HTTP request body instead.
a
I’ve created an issue about documentation.
🙏 1
387 Views