I am writing a file upload application. So it contains a keyname in textbox and a fileupload
control. A save button is used to save the keyname and the file. My issue is when I select a file in fileupload control and I donot enter any value in keyname textbox and click on save button, a message is shown ‘enter a keyname’ but the fileupload control is cleared. How can retain the value in the fileupload control?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can’t retain value in FileUpload control. This is because of security reasons. you can use RequiredFieldValidator for keyname textbox.
Method 2
I don’t think you can assign the value to a FileUpload control for security reasons.
Just think if you could, you could put in by default the path to some known system file that has personal info. Then hide the control on a page and have the user submit the page for whatever reason and auto download files without them knowing.
The file upload control is very locked down because there would just be so many ways to exploit it if it were not.
Have you considered not doing a postback to check if the keyname was entered? Do a check via javascript and abort / show a message if no key value was entered. The postback is what is clearing the value so if you prevent the postback from happening, then your problem will go away as you will not get around the file upload clearing after a postback issue.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0