坚持FileUpload控件值 [英] Persist FileUpload Control Value

查看:232
本文介绍了坚持FileUpload控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更新面板内的asp.net FileUpload控件。当我点击上传按钮,我读了一些code中的文件,如果C没有找到$ C $然后我出了用于选择从下拉列表中的用户ModalPopup,否则上传和文件通过电子邮件发送到$ C $的用户C(这code保存在数据库)。
如果C没有找到$ C $,它显示ModalPopup和删除选定的文件,我想坚持岗位后选定的文件回来。
这是我的code

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>

和按钮上点击

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?

推荐答案

背景:
当使用选择的文件<一href=\"http://msdn.microsoft.com/en-us/library/System.Web.UI.WebControls.FileUpload.aspx\">FileUpload控制,然后回传,<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx\">PostedFile属性,都会与文件 HttpPostedFile 的对象初始化。由于HTTP请求不能保持状态,因此它失去它的状态。

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.

注意:需要FileUpload控件无法与异步postback.So工作回发来获取文件。一种方法是设置触发你的上传按钮,即&LT; ASP:PostBackTrigger&GT; &安培; NOT &LT; ASP:AsyncPostBackTrigger&GT;

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>

和您的上传按钮code:

And your Upload button code:

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

坚持FileUpload控件,您可以存储的值文件上传在会议上共对象,并回传后取回你的会话所需的值。

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

这篇关于坚持FileUpload控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆