C# FileUpload 嵌套在 UpdatePanel 中时不起作用?C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2107952/
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
FileUpload Doesn't Work When Nested In UpdatePanel? C#
提问by Jason
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload onchange="clickTheButton();" ID="FileUpload1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
Button 1 is outside the update panel and the javascript that gets run when a user adds a file in the upload box is this:
按钮 1 在更新面板之外,当用户在上传框中添加文件时运行的 javascript 是这样的:
function clickTheButton() {
document.getElementById('<%= Button1.ClientID %>').click();
}
The problem is simple. FileUpload1.HasFile == false. I don't know why this is, but when I put it in the update panel it ceases to work.
问题很简单。FileUpload1.HasFile == 假。我不知道为什么会这样,但是当我将它放入更新面板时,它就停止工作了。
I have seen some other threads on this. But none of them answer why this is happening, they just point to things you can download.
我在这方面看到了一些其他主题。但是他们都没有回答为什么会发生这种情况,他们只是指出您可以下载的内容。
EDIT: Really my main reason for wanting to do this is so that I can get a ..Uploading File.. Tag to pop up while the client is uploading to the server and once it has completed, display it in a datalist. I just cant get the UpdateProgress to work.
编辑:真的我想要这样做的主要原因是我可以得到一个..上传文件..标签在客户端上传到服务器时弹出,一旦完成,将其显示在数据列表中。我只是无法让 UpdateProgress 工作。
采纳答案by jamone
Basically you just need to make your button do a full postback to send the file. Also make sure that you have this.Form.Enctype = "multipart/form-data"; set in your code, or you can put in that page. AsyncPostbacks don't work with files for security reasons as mentioned, without hacks. (I've never been able to get it to work).
基本上你只需要让你的按钮做一个完整的回发来发送文件。还要确保你有 this.Form.Enctype = "multipart/form-data"; 在您的代码中设置,或者您可以放入该页面。出于安全原因,AsyncPostbacks 不适用于文件,没有黑客攻击。(我从来没有能够让它工作)。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload onchange="clickTheButton();" ID="FileUpload1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
回答by Kyle Chafin
For security purposes, browsers don't let you post files via javascript. Imagine if I could write a little bit a javascript to asynchronously submit the contents of your My Documents folder to my server.
出于安全考虑,浏览器不允许您通过 javascript 发布文件。想象一下,如果我可以编写一些 javascript 来异步提交我的文档文件夹的内容到我的服务器。
So javascript-ish methods of posting the form, like the XMLHttpRequest used by the UpdatePanel, won't work.
因此,像 UpdatePanel 使用的 XMLHttpRequest 一样发布表单的 javascript-ish 方法将不起作用。
This post describes a decent work around if you're on 3.5 SP1. http://geekswithblogs.net/ranganh/archive/2009/10/01/fileupload-in-updatepanel-asp.net-like-gmail.aspx
如果您使用的是 3.5 SP1,这篇文章描述了一个体面的工作。http://geekswithblogs.net/ranganh/archive/2009/10/01/fileupload-in-updatepanel-asp.net-like-gmail.aspx
And this post describes a couple work arounds if you'd prefer not to use the AjaxControlToolkit. http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx
如果您不想使用 AjaxControlToolkit,这篇文章将描述一些变通方法。http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx
回答by Litisqe Kumar
File Upload will not work with a partial post back. So just add this line at your page load
文件上传不适用于部分回发。所以只需在页面加载时添加这一行
ScriptManager.GetCurrent(this).RegisterPostBackControl(this.YourControlID);
Or use PostBackTrigger
.
或使用PostBackTrigger
.
<Triggers>
<asp:PostBackTrigger ControlID="YourControlID" />
</Triggers>
Or You need special AsyncFileUpload control as defined in AjaxControl Toolkit.
或者您需要 AjaxControl Toolkit 中定义的特殊 AsyncFileUpload 控件。
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError"
OnClientUploadComplete="uploadComplete" runat="server"
ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" />
You can check here.
你可以在这里查看。