简体   繁体   中英

File Upload link clears when page reloads, why?

I am using ASP.net(2.0) with VB.NET.

I have a User registration form. On that form the user supply all his contact details and he can upload a image with the normal file upload control in ASP.net.

This is my problem.
If anything goes wrong on the page then i give the User a error message saying what he left out or what went wrong. But the page refresh when that does happen. NOW the link to the image the user selected is gone. NOW when the user fix his error he thinks that he is uploading a picture but he never did because when the page re loaded it removed the link to his image inside the file upload control.

Note, the user don't have to upload a image, so there will be no error when the field is blank.

Anyone have an idea what I must do?

Anyone have an idea what i must do?

Two usual approaches:

  1. Add client-side (JavaScript) form validation so that for the majority of people, an error will pop up before they've submitted the form rather than later, on the server side, by which time the upload will have been lost.

  2. Give each form instance a unique ID. When a file is uploaded on a form with an error, store that file on the server and record in the database that the form with that ID has an attached file. On the second-chance form, include the ID as a hidden field, and visually indicate that the uploaded file has been remembered. On submission, drag out the remembered file and attach it with the new submission.

I don't think there is much you can do. Browsers in the past few years have locked down on the file form field to prevent drive-by uploads. I would have a separate process for uploading the photo.

That behaviour is by design. It is a security restriction imposed by browsers so that all files are uploaded from users' computers only by their explicit action.

If something does go wrong during form submission, you should intimate the user to re-upload his/her file. That is the right way to do it. Think of it as a Transaction (all or nothing).

For security reasons no one can write a value in a file field in HTML. You can upload the file, then report the error, and give the user a way to cancel the upload. Edit: which is what bobince descibes in more detail in another answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM