Hey, I have a doubt. Suppose Im using KMP with can...
# compose-web
k
Hey, I have a doubt. Suppose Im using KMP with canvas web. Now I want to implement file picker so for file picker Im making a composable component seprately. In web compose how can we make / invoke file picker to pick files.
window.showOpenFilePicker(); like this in js we have this api but in kmp cant find this for js
o
You need to call it in your jsMain source set. If it's not available in kotlin then call js code like this:
js("window.showOpenFilePicker()")
k
then how to listen its callback and the data its sending back after chosing file ?
o
js(“window.showOpenFilePicker()“) as Promise<YourTypeHere>
k
Now here type is FileSystemFileHandle this isn’t yet available in k/js so then how to handle this ?
o
Copy code
external interface SomeInterfaceAvailableInWebAPI {
     val property1: SomeType
     val property2: SomeType2
}
so you can provide such a declaration for FileSystemFileHandle
k
I tried to make a data class and external interface of its props but its showing unknown.
Copy code
external interface FileSystemFileHandle {
    val kind: String
    val name: String
}
Copy code
val file = js("window.showOpenFilePicker()") as Promise<FileSystemFileHandle>
file.then {
    selectedFile.invoke(it.toString())
    console.log(it.kind)
}.catch {

}.finally {

}
o
I'm not sure why it's showing "uknown". I can try to reproduce but not today. You can try to implement it in plan js first to be sure. And then to port the js code to k/js
k
Copy code
external interface SomeInterfaceAvailableInWebAPI {
     val property1: SomeType
     val property2: SomeType2
}
so you can provide such a declaration for FileSystemFileHandle can you share me this docs Ill look into it
o
I couldn't find it quickly. But #javascript channel is about k/js specifically
p
Did someone ever get this working?
k
Still waiting;)
p
@John O'Reilly Browsing the internet; it seems you were in search too: https://github.com/Wavesonics/compose-multiplatform-file-picker/issues/101#issuecomment-1875110729 Did you find a solution?
j
No, not yet
p
All right; got something working 😊
Untitled.kt
It’s important to note that the file picker stuff is not available in Firefox for instance. Works in Chrome, Edge and Opera https://developer.mozilla.org/en-US/docs/Web/API/Window/showOpenFilePicker
Here is a more compatible way:
Untitled.py
k
Aye thanks broo
p
Til JS ^^
k
JsAny is how will I get data from it
p
See the first snippet