回复于()在ASP.NET中的正确使用 [英] Correct usage of Response.Write() in ASP.NET
问题描述
我试图强制下载的XML文件,当用户访问一个页面。
I am trying to force a download of an XML file when the user visits a page.
这是在code我使用
public partial class GenerateTemplate : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
//.............
//Going about generating my XML
//.............
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=template.xml");
Response.Write(xmlDoc.InnerXml);
Response.Flush();
Response.Close();
}
}
我现在面临一个问题,就是我的下载窗口无限期挂起,而没有完成下载打开该文件/。
I am facing a problem that my download window hangs indefinitely, without ever completing the download/Open of the file.
我是什么做错了吗?我不能处理任何物体或关闭任何连接这里?
What am I doing wrong? Am I not disposing any objects or closing any connections here?
推荐答案
I've posted a reply to a similar question. To quote myself:
只是一个小除了其他的答案。在下载的最后我执行:
Just a small addition to the other answers. At the very end of a download I execute:
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
我才知道,否则,下载有时不能成功完成。
I learned that otherwise, the download sometimes does not complete successfully.
此谷歌论坛发帖还指出,到Response.End
抛出一个 ThreadAbortException
,你可避免使用 CompleteRequest
方法
This Google Groups posting also notes that Response.End
throws a ThreadAbortException
which you could avoid by using the CompleteRequest
method.
这篇关于回复于()在ASP.NET中的正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!