Phoenix LiveView (Uploads): How to reject a zero-byte file and show validation error

I have created a simple file uploader using Phoenix LiveView, but I have a question regarding entry validation. I have been able to detect files that are too large or have unacceptable file types and output error messages, but I don’t know how to alert the user when a zero-byte file is about to be uploaded.

It appears that Phoenix LiveView simply ignores such files. This may be an intended specification rather than a bug, but I would like to output an error. What should I do?

The version of Phoenix LiveView is 0.17.6.

Zero-byte files are indeed ignored by the live uploader. I am not sure if there’s anything to do on the LiveView side– since the server never gets a file ref for the empty file, it has no place on which to hook the error.

One option might be to verify the files yourself and then add them to the live uploader using the upload methods on the client-side hooks. Just know that this way will require a lot of boilerplate JS code so you can access the files before they are added to the live uploader.

1 Like