如何从服务器下载文件 [英] How to download a file from server
本文介绍了如何从服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友,
我在服务器中有一个Excel文件.而且我有一个Web表单中的按钮.当用户单击该按钮时,它将把该excel文件下载到客户端计算机.
我迫切需要该代码.
我正在使用以下代码
Dear Friends,
I have a excel file in server. And i have a button in a web form. When user clicks that button it will download that excel file to client machine.
I need that code urgently.
I am using this below code
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=Image.xml");
Response.TransmitFile(Server.MapPath("XML/Image.xml"));
Response.End();
它工作正常,但是当我将其保留在更新面板中时无法正常工作.有任何建议.
It is working fine but when i keeping it within Update Panel not working. Any suggestion.
推荐答案
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" >
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</div>
在按钮单击事件上尝试以下操作:
try this on the button click event:
Dim dlScript As String = String.Format("window.open('{0}');", "/XML/image.xml")
ScriptManager.RegisterStartupScript(Page, GetType(String), "fileDownloadScript", dlScript, True)
它应该可以工作...它使用ScriptManager来注册脚本以显示下载对话框
it should work...it uses ScriptManager to register script to show the download dialog
感谢kamalakanta nayak,
我根据您的建议使用了Trigger
Thanks kamalakanta nayak,
I used your suggestion for using Trigger
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
而且效果很好..
and it works fine..
这篇关于如何从服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文