Hi, what is the proper way to upload an image on A...
# multiplatform
h
Hi, what is the proper way to upload an image on Android and iOS using Ktor? I couldn’t find a source code or sample. Also documentation hasn’t explained to how to create a File and convert it to the ByteArray on Android and iOS
s
You can use okio for reading bytes and handing them over to ktor for upload
e
I use this as my request body:
Copy code
class ByteStreamContent(
    private val readChannel: ByteReadChannel,
    numBytes: Long,
    override val contentType: ContentType,
  ) : OutgoingContent.ReadChannelContent() {

    override val contentLength = numBytes

    override fun readFrom() = readChannel
  }
and then to convert the file to a `ByteReadChannel`:
Copy code
context.contentResolver.openInputStream(this)?.toByteReadChannel()
(where this is a
Uri
)