vb.net 无法在 UpdatePanel 中下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22081073/
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
Cannot Download file in UpdatePanel
提问by Computer
The below code works which allows me to download a Word document.....
下面的代码可以让我下载 Word 文档.....
Try
Response.BufferOutput = True
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/msword"
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=myfile.doc")
HttpContext.Current.Response.Write(s)
'HttpContext.Current.Response.End()
HttpContext.Current.ApplicationInstance.CompleteRequest()
HttpContext.Current.Response.Flush()
Catch ex As Exception
Response.Write(ex.Message)
End Try
But as soon as i add an UpdatePanel - it doesnt download the file and no errors are generated? After reading around i added a trigger with the ControlID value set to the button that starts creating the Word doc file. I've tried several combinations of code but nothing seems to work. Any help on how to narrow this down? I've also debugged and no errors show. Ive checked my downloads folder - nothing there, tried setting no cache (Response.Cache.SetCacheability(HttpCacheability.NoCache)) and that didnt work. As soon as i remove the UpdatePanel then all seems to work?
但是,一旦我添加了 UpdatePanel - 它就不会下载文件并且不会生成错误?阅读后,我添加了一个触发器,其 ControlID 值设置为开始创建 Word 文档文件的按钮。我尝试了几种代码组合,但似乎没有任何效果。有关如何缩小范围的任何帮助?我也调试过,没有显示错误。我检查了我的下载文件夹 - 那里什么都没有,尝试不设置缓存(Response.Cache.SetCacheability(HttpCacheability.NoCache)),但没有奏效。一旦我删除了 UpdatePanel 那么一切似乎都可以工作?
<asp:UpdateProgress ID="ProgressUpdate" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<ProgressTemplate>
<img alt="progress" src="../images/loading.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="buttonDownloadFile" />
</Triggers>
<ContentTemplate>
..
Completely lost on this one. Could anyone suggest a workaround or how to tackle this problem?
完全失去了这一点。任何人都可以提出解决方法或如何解决此问题?
采纳答案by sh1rts
The UpdatePanel does not support file upload or download. There are tons of ajax-enabled components out there that will do this, Google is your friend.
UpdatePanel 不支持文件上传或下载。有大量支持 ajax 的组件可以做到这一点,谷歌是你的朋友。
EDIT: -
编辑: -
Some examples: -
一些例子: -
http://forums.asp.net/t/1076322.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview- I like this approach, he injects an IFrame using JavaScript that points to a page responsible for downloading the file. Works inside an UpdatePanel
http://forums.asp.net/t/1076322.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview- 我喜欢这种方法,他使用指向负责下载文件的页面的 JavaScript。在 UpdatePanel 内工作
http://encosia.com/ajax-file-downloads-and-iframes/- Similar approach
回答by Steve Coleman
The accepted answer is just plain wrong. You need to register the control with the scriptmanager. Mine is in the master page and here is the code i use to register any button for proper post backs.
接受的答案是完全错误的。您需要向脚本管理器注册该控件。我的在母版页中,这是我用来注册任何按钮以进行正确回发的代码。
private void MasterPageRegisterButtonForPostBack(Control bt)
{
MasterPage masterPage = Master;
if (masterPage is MainMaster)
{
var mainMaster = masterPage as MainMaster;
if (bt != null && mainMaster.MasterScriptManager != null)
{
mainMaster.MasterScriptManager.RegisterPostBackControl(bt);
}
}
}
回答by Butti
I got it working the following way:
我按以下方式工作:
inside my Update Panel I configured the controls that may forece a full postback in order to get the download working.
在我的更新面板中,我配置了可能强制进行完整回发以使下载工作的控件。
(I'm using also Master pages,this is the same solution as Steve's but registering it in the aspx and not in code behind)
(我也在使用母版页,这与 Steve 的解决方案相同,但将其注册在 aspx 中而不是在后面的代码中)
<asp:UpdatePanel runat="server" ID="UpdatePanelDownload" UpdateMode="Conditional" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:LinkButton ID="LinkButtonDownload" OnClick="Download_Click" runat="Server">Save XML</asp:LinkButton>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="LinkButtonDownlod" />
</Triggers>
</asp:UpdatePanel>
回答by Santiago quits SO
I had to open an ashx Generic Handler in another window and passed some session variables to it, like filename, full path, etc.
我不得不在另一个窗口中打开一个 ashx Generic Handler 并向它传递一些会话变量,如文件名、完整路径等。

