https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
e

Elnur Jeksenov

11/18/2020, 5:17 AM
Hi guys👋 My android project contains three modules: 1. app 2. ui(jetpack compose) 3. sdk(kotlin multiplatform, ktor client) So I’m making requests to server in sdk(kotlin multiplatform) module. I want to upload file via multipart-form-data, but there is no support for java.io.file in kotlin multiplatform. Have you got any idea to solve this problem?
a

Andrew

11/18/2020, 6:13 AM
I have not used a lot of the things in this setup but have an idea of how it might work. How would you send some random bytes of data from a byte[] array?
If that works then there might be some other library to handle files in KMP or you can just write your own file class using expect/actual. I did similar for java.util.date recently.
Basically each platform needs to do its implementation of file io (via library or you code it) which can abstract it so KMP can use it. ktor client is doing that for the network layer
e

Elnur Jeksenov

11/18/2020, 6:41 AM
I didn’t get your question. I have tried to use this library, there is no files support. It’s unavailable yet.
a

Animesh Sahu

11/18/2020, 8:18 AM
In common create
expect class File { /* Bunch of utility ... */ fun abc(): ReturnType }
It behave similar to interface In JVM do
actual typealias File = <http://java.io|java.io>.File
In iOS or something like that do the same In native use Posix API and create your own File class implementing
actual class File {...}
e

Elnur Jeksenov

11/18/2020, 11:05 AM
Copy code
<http://httpClient.post|httpClient.post>(url) {
            body = MultiPartFormDataContent(
                formData {
                    this.appendInput(
                        key = "first_file_uploaded",
                        headers = Headers.build {
                            append(HttpHeaders.ContentDisposition,
                                "filename=$fileName")
                        }
                    ) {
                        buildPacket { writeFully(byteArray) }
                    }
                }
            )
        }
Copy code
kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found.
    Mark the class as @Serializable or provide the serializer explicitly.
there is exception. Have you got any idea to make ‘MutliPartFormDataContent’ serializable?