C# 持久化文件上传控制值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18651039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persist FileUpload Control Value
提问by Syed Salman Raza Zaidi
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.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>
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?
如何在回发时保留上传控件的值?
采纳答案by R.C
Background:: When a file is selected using FileUploadControl ,then on postback, PostedFileproperty gets initialized with HttpPostedFileobject for the file. Since http request cannot maintain state, so it looses it's state.
背景::当使用FileUploadControl选择文件时,然后在回发时, PostedFile属性将使用文件的HttpPostedFile对象进行初始化。由于 http 请求无法保持状态,所以它失去了它的状态。
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>
注意: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>
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.
为了保持 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;
}
}
回答by Pawan
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
要使其工作,您应该使用 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>
回答by Fernando Rodrigues
try add
尝试添加
$('form').attr('enctype', 'multipart/form-data');