13

我在更新面板中有 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();
    }
}

如何在回发时保持上传控件的值?

4

3 回答 3

21

背景::当使用FileUpload Control 选择文件时,然后在回发时, PostedFile属性将使用文件的HttpPostedFile对象进行初始化。由于http请求不能保持状态,所以它失去了它的状态。

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

还有你的上传按钮代码:

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

为了保持 FILEUPLOAD CONTROL 的值,您可以将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; 
         }
     }
于 2013-09-06T11:25:22.293 回答
4

这个问题有据可查,更新面板被列为不适用于某些控件。

文件上传和树状视图是其中的两个。

要使其工作,您应该使用 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>
于 2013-09-06T13:48:37.090 回答
-1

尝试添加

$('form').attr('enctype', 'multipart/form-data');
于 2018-09-21T12:49:30.407 回答