2

Hi,

It seemse like uploading of files is a tricky thing even in ASP.NET MVC2.

I have placed a couple of intput(type file) on my form and when submitting thay are mapped to my viewmodel nicely. The problem is that if the ModelStat.IsValid is false then we will be returned to the same form and the fileinputs will be empty?

I know that its possible to clear a input field(by replacing it) but I do not see how I could set the input properly?

The users have to be able to see/change/remove the file upload even when returning to the form after a faild validation on the service, How can I handle this?

BestRegards

1
  • I know ,the post is very old.If you have ajax request,input won't be blank when model.state is invalid,if it http post request then input will be blank.yes file input cannot be populated .password input also get blank for same reason. Commented Apr 5, 2017 at 4:13

1 Answer 1

4

Browsers will not allow you to prepopulate file input elements. If they did, then malicious web applications could just start uploading random files from the site visitor's hard drive to the server.

Generally the way this is handled is that the file is saved to a temporary location on the server, and when the inputs are corrected and the form reuploaded the server can associate the new incoming data with the existing temporary file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is true and I will have to come up with a smart solution here. My thought is to upload the images at the fest submit, if there is a validation problem then the returning viewclass will have to contain information about the uploaded images. On the client side I will have to use javascript to handle the upload and existing images. Alot of work but I do not see any other way (becides flash).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.