保持FileUpload控件值

时间:2013-09-06 06:09:20

标签: c# asp.net

我在更新面板中有asp.net FileUpload控件。当我点击上传按钮时,我正在读取一些代码的文件,如果找不到代码,那么我显示ModalPopup从下拉列表中选择用户,否则上传文件并通过电子邮件发送给该代码的用户(此代码保存在数据库中) 。 如果找不到代码,它显示ModalPopup并删除所选文件,我想在回发后保留所选文件。 这是我的代码

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

并按下按钮

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

如何在回发后保留上传控件的值?

3 个答案:

答案 0 :(得分:16)

<强>背景: 使用FileUpload Control选择文件时,在回发时,PostedFile属性将使用文件的HttpPostedFile对象进行初始化。由于http请求无法维护状态,因此它会丢失它的状态。

注意: FileUpload控件不能用于异步回发。因此需要回发来获取文件。一种方法是为您的上传按钮设置触发器,即<asp:PostBackTrigger >&amp;不是<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>

您的上传按钮代码:

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

要理解文件控制的价值,您可以在会话中完全存储fileupload对象,并在回发后从会话中检索所需的值。

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

答案 1 :(得分:3)

此问题有一定记录,更新面板列为不使用某些控件。

文件上传,树视图是两个巨人。

要使其工作,您应该使用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>

答案 2 :(得分:0)

尝试添加

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