【发布时间】:2016-01-04 08:06:27
【问题描述】:
可能是一个重复的问题。但我的情况有所不同。我有一个页面来下载文本文件。在页面中,我首先将文本文件解密为string plainText。然后将该字符串写入文件并上传到名为 Decrypted Files 的文件夹。下载解密文件并删除以前保存的文件。
这是我的下载代码
//Write the decrypted text to folder in the server.
System.IO.File.WriteAllText(Server.MapPath("~/Decrypted Files/" + FileName), plainText);
//Code for Download
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename='" + FileName + "'");
Response.WriteFile(Server.MapPath("~/Decrypted Files/" + FileName));
//Delete File from the folder
if (System.IO.File.Exists(Server.MapPath("~/Decrypted Files/" + FileName)))
{
System.IO.File.Delete((Server.MapPath("~/Decrypted Files/" + FileName)));
}
Response.End();
但是代码执行不会从Response.End(); 继续,并且 .aspx 页面没有完成加载。我的代码有什么问题?
【问题讨论】:
-
为什么不运行项目并启用异常设置/设置调试点?问题可能来自其他行而不是 Response.End。也看到这个答案 - stackoverflow.com/a/18477989/2010289
-
"The code execution not continue from Response.End()" - 你能澄清一下这句话的意思吗?显然你知道
Response.End会抛出 ThreadAbortException,所以你不希望代码 在 那行之后执行......那么你在说什么代码? -
改用 Response.TransmitFile
标签: c# asp.net download httpresponse