Hello. I couldn't find how to send multipart form ...
# multiplatform
r
Hello. I couldn't find how to send multipart form in Kotlin Multiplatform using Ktor and native "files". Issue is that now I'm reading javascript File to ByteArray and Android / iOS File to ByteArray which 1. Takes a lot of time 2. Might cause OOM Is there a way do to it with streams? I saw that append method in Ktor has options like InputProvider or ChannelProvider or Source but I have no idea how to use it
a
Unfortunately, you have to implement an adapter that reads a file asynchronously for each platform. On iOS, the native
dispatch_read
method can be used for that (https://stackoverflow.com/a/78736380/13963150).
r
And which abstraction should I use for my adapter? Any of those three i mentioned is okey? One of them if preferred?
a
You'd better use the
ChannelProvider
, although you'll have to slightly change the implementation for iOS. Unfortunately, browser environments don't support request body streaming, so the only solution with Ktor is to read the entire body into memory.
r
So when we send form in plain JS / html environment with files then all files are loaded into memory? It might be silly question but I wasn't doing frontend development before Kotlin JS
a
No, they are not. The browser knows how to stream files and similar objects it operates on, but does not provide a generic interface to adapt any object for streaming. The problem is that the multipart methods reside in the common module, which is unaware of the platform-specific objects.
r
I see, so if we are using Kotlin JS for forms then we have problem? 😉 I would have to rewrite sending part to plain JS?
a
That's correct.
r
But can I still use Kotlin and fetch API?
a
Yes, you can.
r
Thanks, I will try to use some expect actual magic to handle it on browser site