Asp.net File Upload Control Loses File On Postback

Active3 years, 1 month ago
  • FileUpload control clears itself. Browse other questions tagged asp.net file-upload controls postback or ask your own question. 9 years, 6 months ago. 13,768 times. 2 years, 6 months ago. Get FileUpload value onchange in ASP.NET.
  • May 01, 2008  Now on click of upload button, I am able to read the file but after the upload processing completes, the file name written in the text area of control is lost. Can you suggest any workaround by which we retain the file selected by user after the postback in the text area of File upload control.

I'm having some trouble with the FileUpload control and was wondering if I could get some help.

In my griview, i have Fileupload template field. But, problem is whenever my page get postback fileupload loses its content or selected file. Please give me solution for this Problem. FileUpload control loses its contents after PostBack. Problem is whenever my page get postback fileupload loses its content or selected file. Please give me.

On my page I have a FileUpload control, and a drop down list.

So the user browses to the file they want, and then select an option from the drop down (which enables some checkboxes that are also on the page for use, depending on what they select in the drop down). This causes the FileUpload control to become empty, and now the user must browse to the file they wanted again.

Is there anyway to prevent the FileUpload control from losing its contents during the PostBack?

skaffman
352k90 gold badges745 silver badges733 bronze badges
Denis SadowskiDenis Sadowski
2,1924 gold badges20 silver badges25 bronze badges

7 Answers

Since you tried Relster's suggestion and it didn't work, Spencer is correct. Due to security concerns, it is impossible for anything but the browser to set the path in an <input type='file'/> element. The only solution is to restructure the flow so that the only postback is done when you want to submit the file, and do any other manipulation with client side scripting.

The reason you cannot set the path is because it would allow you to steal users' files. For example, if you hide the input, and have an innocent looking button for postback, you could set the default path of the file input to whatever you wanted to get access to, and the user would upload it without ever knowing what's going on.

SeanSean

So, if I understand correctly, the drop down has the autopostback property set to true and you're checking the value of the drop down to make default changes to the check boxes?

Asp net mvc image upload

Instead try using client side scripting to do the trick.

Spencer RuportSpencer Ruport
31.5k11 gold badges77 silver badges130 bronze badges

I will most likely be using client side scripting to fix the issue.

However I've found an solution that makes use of ajax for those people who might be interested.

From the post by Jessica Cao, which also contains a code example:

'... you could use AJAX to make asynchronous postback to the server, make the partial page which contains the DropDownList to postback, and the page contains the FileUpload control won't postback, so the FileUpload will maintain the FilePath.'

This method requires the use of System.Web.Extensions.dll, and as a result the .Net 3.5 Framework.

Denis SadowskiDenis Sadowski
2,1924 gold badges20 silver badges25 bronze badges

Whenever there's a postback you have to check whether there was also a file coming from the client and save it. You won't be able to re-fill the input box on the client though. I suggest you better hide file upload control (unless you allow multiple file upload) and display filename with check mark beside it. Similar to GMail when you upload files to your email message.

Reasons are security concerns as other pointed out.

I'd also disable any auto postbacks because a drop down change may seem to last an eternity to the client if the upload file size is sufficiently large.

FocusRobert KoritnikRobert Koritnik
79.3k43 gold badges246 silver badges368 bronze badges

You can't do this. A simple Solution is to move to Ajax file Control.

Cosmin Pirvu
19.5k5 gold badges35 silver badges56 bronze badges
NilangNilang

I wrapped my autopostback controls inside an asp:updatepanel and that took care of it except for validation failure.

MaslowMaslow
13.1k15 gold badges88 silver badges173 bronze badges

The simple solution for preventing file loss from uploadcontrol while postback is to put uploadcontrol outside the updatepanel control on a .aspx page.

eg.

Mauricio Gracia Gutierrez
5,8213 gold badges40 silver badges61 bronze badges
Pradeep atkariPradeep atkari
3351 gold badge5 silver badges14 bronze badges

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

Mvc Upload File Example

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:

C# File Upload Mvc

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.

ManishManishLoses
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).

Asp Net Mvc Image Upload

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
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Asp Fileupload Control

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

Comments are closed.