13

I have asp.net FileUpload control inside an update panel. When I click upload button, I am reading the file for some code, if code not found then I am showing ModalPopup for selecting a user from dropdown, otherwise uploading and emailing the file to user of that Code(this code is saved in Database). If code not found,its displaying ModalPopup and removing the selected file, I want to persist the selected file after post back. This is my code

<asp:UpdatePanel ID="UpdatePanel3" runat="server" >
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server"  />
        <asp:RequiredFieldValidator ID="rfvFileupload" ValidationGroup="validate" runat="server" ErrorMessage="* required" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
    </ContentTemplate>
</asp:UpdatePanel>

and on Button Click

protected void btnupload_Click(object sender, EventArgs e)
{
    //Reading the file and Checking from Database
    if(codefound)
    {
        //Sending email to the user of the Code
    }
    else
    {
        ModalPopupExtender1.Show();
    }
}

How can I persists the value of Upload control on post back?

tom redfern
  • 30,562
  • 14
  • 91
  • 126
Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87
  • Consider (arguably more simple) suggestion in https://stackoverflow.com/questions/1111428/how-to-prevent-fileupload-control-from-clearing-on-postback 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 – Michael Freidgeim May 15 '23 at 08:58

3 Answers3

22

Background:: When a file is selected using FileUpload Control ,then on postback, PostedFile property gets initialized with HttpPostedFile object for the file. Since http request cannot maintain state, so it looses it's state.

NOTE: FileUpload control will not work with asynchronous postback.So a postback is needed to get the file. One way is to set the triggers for your Upload button, i.e. <asp:PostBackTrigger > & NOT <asp:AsyncPostBackTrigger>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
      <asp:FileUpload ID="fileUploadImage" runat="server"></asp:FileUpload>
      <asp:Button ID="btnUpload" runat="server" Text="Upload Image" 
           OnClick="btnUpload_Click" />
      </ContentTemplate>
      <Triggers>
          <asp:PostBackTrigger ControlID="btnUpload"  />
      </Triggers>
 </asp:UpdatePanel>

And your Upload button code:

 protected void btnUpload_Click(object sender, EventArgs e)
     {
        if (fileUpload1.HasFile)
        {                
            fileName = fileupload1.FileName;
            fileUpload1.SaveAs("~/UploadedContent/" + fileName);
        }
     }

TO PERSIST THE VALUE OF FILEUPLOAD CONTROL, you can store the fileupload object altogether in session and after postback retrieve the values you require from session.

protected void Page_Load(object sender, EventArgs e)
    {
        // store the FileUpload object in Session. 
        // "FileUpload1" is the ID of your FileUpload control
        // This condition occurs for first time you upload a file
         if (Session["FileUpload1"] == null && FileUpload1.HasFile)  
           { 
            Session["FileUpload1"] = FileUpload1; 
            Label1.Text = FileUpload1.FileName; // get the name 
           }
        // This condition will occur on next postbacks        
        else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile)) 
          { 
            FileUpload1 = (FileUpload) Session["FileUpload1"]; 
            Label1.Text = FileUpload1.FileName; 
          } 
     //  when Session will have File but user want to change the file 
     // i.e. wants to upload a new file using same FileUpload control
     // so update the session to have the newly uploaded file
        else if (FileUpload1.HasFile) 
         { 
            Session["FileUpload1"] = FileUpload1; 
            Label1.Text = FileUpload1.FileName; 
         }
     }
R.C
  • 10,417
  • 2
  • 35
  • 48
  • 3
    Here 2nd part of above answer actually answers your questio: How to persist FileUpload Control vale. The first part just shows how to make FileUpload control work inside UpdatePanel. Since you didn't mentioned any issue with this, I hope this part is working already for you. – R.C Sep 06 '13 at 14:32
  • I tried doing this and received an error that the fileupload object is non-serializable and that it can not be stored in session. – Stan Shaw Jan 21 '20 at 18:57
  • it may be possible that SessionMode is set to SQLServer or StateServer , Can you try change it to InProc and see if its works ? – R.C Jan 23 '20 at 05:08
4

This problem is somewhat well documented, the update panel is listed as not working with certain controls.

File upload, and tree view being 2 of the biggies.

To make it work you should use Triggers/PostbackTrigger

<asp:UpdatePanel ID="UpdatePanel3" runat="server" >
  <ContentTemplate>
    <asp:FileUpload ID="FileUpload1" runat="server"  />
    <asp:RequiredFieldValidator ID="rfvFileupload" ValidationGroup="validate" runat="server" ErrorMessage="* required" ControlToValidate="FileUpload1" />
    <asp:Buton ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click"></asp:Button>
  </ContentTemplate>

  <Triggers>
    <asp:PostBackTrigger   ControlID="btnupload"/>
  </Triggers>
</asp:UpdatePanel>
Pranesh Janarthanan
  • 1,134
  • 17
  • 26
Pawan
  • 1,065
  • 5
  • 10
-1

try add

$('form').attr('enctype', 'multipart/form-data');