Fail
09/11/2019, 12:18 PMval result = <http://client.post|client.post><HttpResponse>("<http://127.0.0.1:$port/handler>") {
body = MultiPartContent.build {
add("user", "myuser")
add("password", "password")
add("file", byteArrayOf(1, 2, 3, 4), filename = "binary.bin")
}
}
But what will doing if file is big? ByteArraySize could not be more than free RAM. How to send file from IOS and Android to multiplatform without out of memory errors?ribesg
09/11/2019, 12:38 PMprivate suspend fun doUploadFile(data: ByteReadChannel, uploadUrl: String, contentType: ContentType): Unit =
http.put(uploadUrl) {
body = object : OutgoingContent.ReadChannelContent() {
override val contentType = contentType
override fun readFrom() = data
}
}
Fail
09/11/2019, 12:39 PMribesg
09/11/2019, 12:42 PMUri
to a local file, the data
parameter is appCtx.contentResolver.openInputStream(uri)?.toByteReadChannel() ?: error(...)
ContentType
I’m using ContentType.fromFilePath(fileName).first()
Fail
09/11/2019, 12:45 PMribesg
09/11/2019, 12:46 PMprivate fun NSURL.toByteReadChannel(errorHandler: (Throwable) -> Unit): ByteReadChannel =
writer(CoroutineExceptionHandler { _, e -> errorHandler(e) }, true) {
memScoped {
val bufferSize = 8192
val buffer = allocArray<UByteVar>(bufferSize)
val input = NSInputStream.inputStreamWithURL(this@toByteReadChannel)
?: throw Error("Failed to create NSInputStream from url ${this@toByteReadChannel}")
var result: Int
try {
input.open()
do {
@Suppress("EXPERIMENTAL_API_USAGE")
result = input.read(buffer, bufferSize.toULong()).toInt()
if (result > 0) {
channel.writeFully(buffer.readBytes(bufferSize))
}
if (result < 0) {
throw Error("Failed to read inputStream: ${input.streamError?.debugDescription}")
}
} while (result > 0)
} finally {
channel.close()
input.close()
}
}
}.channel
Fail
09/11/2019, 12:48 PM