How do I upload a file by using `ktor-client` in `...
# ktor
a
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
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
So I have to read the bytes from the client first?
e
what does “read the bytes from the client” mean
i thought you said you were using
ktor-client
?
a
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
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
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
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
Much thanks. Let me dig through and see how I can get a ByteReadChannel
271 Views