Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

c# - Persist FileUpload Control Value

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; 
         }
     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...