I have a form with two inputs: one string, one file.
<form:form ...>
<form:input type="text" ....>
<form:input type="file" ....>
</form:form>
If the validation fails on the string input, the controller routes back to this same view. When that happens, the string field is preserved but the selected file is lost.
Is there a way to preserve the file selected when the view is re-rendered?
I think the answer is no - and that it is intrinsic to the HTML file input, not the server-side framework. Asking in case there's something I'm missing.
It is true that the file input value cannot be preserved. Thats just as it is implemented in all browsers. As far as i can tell it has to do with potential security risks but i never went deeper.
There is a possibility to achieve something similar though. This is what we did in our project:
This works stable now and as a nice side effect the controller was somehow easier and in my eyes also cleaner to write since we do not need multipart for these forms anymore and another controller just handling uploads.
Edit: - you might want to implement a cronjob to clear abandonned uploads!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With