Asp.net File Upload Control Loses File On Postback

Active5 years, 7 months ago
  1. Asp Fileupload Example
  2. Asp Net Upload Image
  3. Asp.net File Upload Control Loses File On Postback

I have a problem regarding a ASP.NET fileuploader that is empty after postback. I have one fileupload control, one textbox control and one button control.

Disclaimer: This site is started with intent to serve the ASP.Net Community by providing forums (question-answer) site where people can help each other. The content posted here is free for public and is the content of its poster. Re: FileUpload control looses contents on PostBack Sep 06, 2007 04:29 PM| mgodoy_desenv| LINK I already read about that (and a lot of another crazies stuffs of FileUpload), cause that is happens to security reason.

Hi, I am using C# and ASP.NET 4.0. I have added a file upload control and a dropdown box in my aspx page. When a file is selected in the file upload and if a postback happens in dropdown or any other scenario, the data in the file upload control is lost. The FileUpload control is not supported with ASP.NET AJAX and Asynchronous Postbacks. They require a full postback due to the nature of how a file upload works (multipart form data).

When I click the button, it checks if the textbox is populated, if it is it performs actions, if its not then it shows an error message and stay on the page. However, when I press the button and the textbox is empty, the button postsback and the fileuploader is empty and the user has to select the file again. So: User selects file > The file is located in the fileuploader > the user presses the button > the error message pops up > the fileuploader loses the file after postback.

Apr 23, 2013  Hi, I have fileupload control in webpage and couple of control with autopostback true, if you select a file in fileupload control and page postback. About ASP.Net's File Upload Control File upload control has a text box and a browse button. When a file is selected, on PostBack PostedFile property gets initilized with HttpPostedFile object for the file.

When the user selected a file but not yet clicked the button (koala.jpg)

When the button is pressed, the file is gone (no file selected)

How do I keep a file in the fileuploader after a postback or maybe tell the button not to post back if the textbox is empty?

EDIT:

Be aware that I simplified the question and that the content of the first post are just made up to make my question more clear.

Page load:

Button Click:

errorSubject & errorContent are hidden divs that show up as the error

Niek Jonkman
Niek JonkmanNiek Jonkman
2333 gold badges8 silver badges20 bronze badges

2 Answers

It's not possible to set the value of a file input. This is a browser security feature, it's the same with a password field, the only difference is with a password field you can set the value using JavaScript.

However with the file input you cannot set the value.

The solution is to prevent the postback, use JavaScript to check the value before posting back.

PhillAsp.net File Upload Control Loses File On PostbackControlPhill

Asp Fileupload Example

14k5 gold badges48 silver badges93 bronze badges

As Phill pointed out you cannot update this Control at all from the code behind or from repopulating from the Form Values.

If you are relying on an auto postback to change data somewhere else on the form, you will need to handle the initial FileUpload and create a fake placeholder that indicates the file name that was/is to be uploaded.

The easier solution might be to wrap the other controls that require the AutoPostBack in a UpdatePanel so that they can be post back independent of the File Upload control.

SteveSteve
1,7122 gold badges12 silver badges23 bronze badges

Not the answer you're looking for? Browse other questions tagged c#asp.netfile-upload or ask your own question.

Active7 years, 9 months ago

I'm having a FileUpload control on a aspx page inside a UpdatePanel with a button on click of which I want to set a label with the filename of the selected file.

Here is the code I have:

ASPX PAGE:

Code Behind:

Every time I hit the button, I'm getting an empty string. What am I missing?

Also, when I do this without UpdatePanel it works fine.

Comments/help appreciated.

ManishManish
2,65417 gold badges51 silver badges86 bronze badges

1 Answer

The FileUpload control is not supported with ASP.NET AJAX and Asynchronous Postbacks. They require a full postback due to the nature of how a file upload works (multipart form data).

The following question should have useful answers: FileUpload control inside an UpdatePanel without refreshing the whole page?

Community
jristajrista
27.8k10 gold badges79 silver badges123 bronze badges

Asp Net Upload Image

Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Asp.net File Upload Control Loses File On Postback

Not the answer you're looking for? Browse other questions tagged asp.netfile-uploadupdatepanelpostback or ask your own question.