views:

1129

answers:

2

I've got a simple form in an MVC app I've made. It contains a file field so users can upload an image. It all works great.

Problem is, if the form submit fails validation the content of the file field is lost (other fields remain populated, thx HtmlHelpers!). How do I keep the file field populated after a failed validation?

TIA!

A: 

As far as I know you cannot set the value of a HTML file input box. I would suggest coupling the file input box with a label or text box.

Then you can populate the it with the value from the file input box to be resubmitted later.

Michael
Interesting... do you know of a site that has implemented it in this way? Looking for examples... thank you.
Chad
Gmail uses something similar with its attachments. Once the file is uploaded it turns text. However this is done asynchronously using AJAX.
Michael
+4  A: 

Browsers are designed in such way because of security risks. It's impossible to set value of file input box in HTML source or by Javascript. Otherwise malicious script could steal some private file without user attention.

There is an interesting information about the subject.

Alexander Prokofyev
yup, I understand the security principle. Just wish it was possible for the scenario I described, simply because I've had the user experience impacted by it. ;/
Chad