https://kotlinlang.org logo
Title
a

andylamax

08/15/2020, 4:46 PM
How do I upload a file by using
ktor-client
in
multiplatform
? I am aware of the
formData{}
but I can't seem to find where I can
append
a
file
to the builder.
e

edenman

08/15/2020, 9:22 PM
can’t help you if you’re trying to do a multipart form upload with a file. if you’re just doing a simple upload you can set a
ByteStreamContent
or
ByteArrayContent
as the request
body
a

andylamax

08/15/2020, 9:50 PM
So I have to read the bytes from the client first?
e

edenman

08/15/2020, 10:00 PM
what does “read the bytes from the client” mean
i thought you said you were using
ktor-client
?
a

andylamax

08/16/2020, 6:01 AM
Yes, I am using ktor client. You said I can use ByteStreamContent or ByteArrayContent. My app requires a user to pick a file and upload it. How do I convert from a file to ByteArrayContent/ByteStreamContent? Do I read the file's content as ByteArray on the client before sending it to the server?
e

edenman

08/16/2020, 10:05 AM
yes
but it’ll be better on memory usage to go
ByteStreamContent
so you don’t have to load the whole thing into memory first
context.contentResolver.openInputStream(uri).toByteReadChannel()
and then pass that to
ByteStreamContent
(but again, if you’ve got a multipart form and you need to upload a file to that, it may be different)
a

andylamax

08/16/2020, 10:08 AM
That would surely do. Thanks again
I can see that working for android client.
What a bout a Blob? or File from the browser? there is no
openInputStream()
e

edenman

08/16/2020, 10:14 AM
yeah, i only have experience on the android side, sorry
but in general there should be some way to get a ByteReadChannel from whatever you’ve got
a

andylamax

08/16/2020, 10:16 AM
Much thanks. Let me dig through and see how I can get a ByteReadChannel