Hi all,
Does anyone know how to upload a file in a KotlinJS front-end and post it to the server?
In JS this is pretty straightforwards, see for example:
Pure JS file upload
In that example, the key is to create a FormData object and attach the File object retrieved from the file selection event to it:
Copy code
formData.append("file", fileupload.files[0]);
The problem I have is that the equivalent FormData class in KotlinJS can have a org.w3c.files.Blob attached to it, of which org.w3c.files.File is a subclass, however, the selection event returns a web.file.File object.
How can I turn the web.file.File into an org.w3c.files.File so that I can attach it and post it?
Thanks
m
Mike Dawson
01/01/2023, 7:54 PM
Hi - we're able to cast it... our event handler is old and imperfect, but it is working as follows:
onFileInputChangedHandler :(Event) -> Unit = {
stopEventPropagation(it)
handleSelectedFile(it.asDynamic().target.files[0] as File)
}
Here the cast is to org.w3c.files.File