I haven’t tested this, but I was looking at how file inputs work and it basically keeps the File object from JS around.
One way you could work with that is using createObjectURL to get a blob: URL that you can use to reference the file later on. You could send the blob URL to Elm, and when it’s time to send it to the server send info out a port (including the blob: url), and have it do the upload from JS by reading the blob. We do something similar for this where we send the real File object and the Blob URL in from a custom element for file upload, and we use that blob URL as a src for image previews.
We’re going the other way through the port, and this is essentially what we’re doing. But as I said in the initial post, it’s unreliable with large files.
It’s lukewestby/elm-http-builder, but the team took a vote and voted against refactoring to remove (I had already started working on a replacement). Regardless, we have a project coming up where we have to send through a port to send a blob to an api that isn’t wrapped by a core library or exploration yet (indexedDB), and I’m sure there’s other instances where it will come up.
No benefit comes to my mind but maybe there is something I’m missing.
There is a tradeoff involved. The approach you linked has a simpler decoder but it pays for this by making you deal with ids. The approach I linked solves the issue without ids at the expense of a more complex decoder.
Oh, and welcome to the Elm Discourse. May you find all the answers you seek.
Thank you for welcoming
Yeah, I’m quite a newbie. The thing I still don’t understand - is it possible to avoid base64 overhead when uploading arbitrary file/data to a server?
Some time ago I wrote file uploading (to a server) app based on the approach I linked which uses base64 and I guess it is a requirement there. Consequently, file contents are encapsulated in json. However, your example has no base64 by default and it’s not clear for me if it’s really suitable for uploading arbitrary files to a server.
Ah, my fault was that I wasn’t aware of the elm/file package and its purpose. It’s a relatively new package (appeared half a year ago). It looks like major progress over several custom solutions that were flying around the internet.
And in contrast elm/file package uses I guess some implicit native code approach without exposing JS internals to the programmer, right?
Maybe a bit offtopic, but I couldn’t find an explanation on how project sources like Elm/Kernel/File.js get involved in the build? Is it the case when the package is trusted (couldn’t see any notion on this) and allowed to use native code?