Hi all, Does anyone know how to upload a file in ...
# javascript
t
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
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
c
Here's how I did it using Ktor and multipart requests (Kotlin/JS + Kotlin/JVM): https://gitlab.com/opensavvy/formulaide/-/commit/100b29c0c43623ef2fae875a9cfbf9fdbe03becd
163 Views