Hi @caspg! Can you describe a bit more about what you want to do with the image metadata?
Generally our guidance here is that you cannot trust data that comes from the client without verifying it on the server. So if you send width/height/etc from the client you still need some way to verify the information.
Are you uploading to your web server or to a cloud storage provider?
Using this I’m getting an empty file list.
I am a little surprised by this. LiveView listens to form events on window, so I would expect any files added by the change event to still be available until LiveView consumes the input event.
I’m sending the width and height and storing them in DB. Images are stored in S3.
I know that we can’t trust things that are coming from the client. In my case, it’s not really an issue even if someone messes with it. The worst thing that can happen is that photo will render incorrectly. I’m also reading GPS data from Exif info.
I could send an image to the server and read all of that there but that would require bigger servers. Right now, I’m using the cheapest Heroku dyno and serving ~80k monthly unique users. When I was processing images on the server I was running out of memory from time to time.
Okay, I tested it more and an empty file list is only when using drag and drop. When I’m selecting files using “regular” input the list is populated.
EDIT:
It looks that drag and drop is dispatching “input” event not “change”. But still file list is empty.
Nice, thanks Why we don’t assign files to input.files on drag and drop anyway?
Right now, I will need two listeners that are reading files differently. Or is it an expected behavior in drag and drop? It’s first time I’m implementing this
In the LiveView client we listen for ‘drop’ events on phx-drop-target elements, collect the files, and then emit an ‘input’ event to trigger the form change to the server. We maintain our own file list for bookkeeping purposes, otherwise we risk input.files getting out-of-sync with uploads to the server.
Yes input.files is a property on an HTMLInputElement whose type is file (Getting information about selected file(s) | MDN). It is updated each time a user selects files from the input. So if we only relied on it we would lose any existing files whenever the user selected new files! LiveView maintains its own list so we can do things like add files from drop targets as well as safely handle enhancements like auto-uploads, live image preview, and allow users to build up a “queue” of files before submitting.