2012-01-16 69 views
0

我用c#创建了一个xml文件,我可以将它保存在我的机器中的任何位置(在这种情况下,我已将它保存在名为“temp.xml”的应用程序的根目录下),但我想让它让用户从他们的浏览器下载购买链接 - > “点击这里下载文件。”使用asp.net下载xml文件

在Chrome和FireFox中,它显示一个新选项卡,其中只有一些值位于xml文件的正文部分,但IE显示了整个xml。当有人点击上述链接时,我想将其下载到我的下载文件夹中。

在此先感谢您的支持。

+0

我编辑了我的答案。希望有所帮助。 – IsmailS 2012-01-16 10:41:58

回答

1

您需要特别提及文件类型和名称。并使用TransmitFile方法。这将显示Save As窗口。

我从this webpage

Response.ContentType = "image/jpeg"; 
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg"); 
Response.TransmitFile(Server.MapPath("~/images/sailbig.jpg")); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
+0

感谢您的回复先生,当我尝试下面的代码,它给了我错误 base {System.Exception} = {无法评估表达式,因为代码已优化或本机框架在调用堆栈之上。} – tanweer 2012-01-16 10:15:02

+0

尝试使用'HttpContext.Current.ApplicationInstance.CompleteRequest()'而不是'Response.End()'方法。我已经更新了这个问题。先生,不用给我打电话! :) – 2012-01-16 10:33:24

+0

感谢您的时间。实际上我使用WebMethod,它是静态的,所以它不工作。 – tanweer 2012-01-16 11:34:12

1
Response.AddHeader("content-disposition", "attachment;filename=temp.xml"); 
Response.ContentType = "text/xml"; 
Response.Write(File.ReadAllText(Server.MapPath("~/temp.xml"))); //you may want to write the XML directly to output stream instead of creating an XML file first. 
Response.End(); 

希望这有助于得到了下面的代码。