upload component - warn the user if upload button not pressed

Hi,

I would like to warn the users when in a form they have selected a file but they submit the form without pressing the upload button. I haven’t been able to find anything that tells me if the user has browsed any file or not. How can I achieve this?

So I guess I have no way of knowing that…

The thing is that if the user selects a file but doesn’t upload it (yet) this information doesn’t get send to the server side of the upload component. So the only way to get it would be maybe by extending the client side of the component and add some way to get if a file is attached to the code.
Though i’m not sure if that’s even possible because of the way the browser and Vaadin handle security when uploading files. For example it’s not possible to get the FilePath from the selected file for security reasons.

I would recommend handling these kind of situations by using an immediate upload. This way when the user selects a file the server side immediately (specifically when the file finished uploading) knows that there is a file. When the user then discards the form you can warn him.

Hope this helps.

That was very useful to learn how things work Marius. I had already set immediate to true as the alternative solution and I hope this will be ok with the client.

Thank you very much for the explanation!

HTML file inputs support the onchange event, so technically this is possible. Vaadin doesn’t currently have explicit support for them, but there’s a recent
enhancement ticket
proposing implementing the functionality.

Luckily, if you’re prepared to delve a bit into the world of extending Vaadin, you can quite easily write an
Extension
for the Upload component to do what you need, without any changes required in Vaadin core. A
JavaScriptExtension
is probably the easiest way.