Persist FileUpload Control Value

前端 未结 3 1184
渐次进展
渐次进展 2020-11-30 10:21

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

相关标签:
3条回答
  • 2020-11-30 10:56

    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; 
             }
         }
    
    0 讨论(0)
  • 2020-11-30 11:08

    try add

    $('form').attr('enctype', 'multipart/form-data');
    
    0 讨论(0)
  • 2020-11-30 11:16

    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>
    
    0 讨论(0)
提交回复
热议问题