这可能是一个重复的问题。但我的情况有所不同。我有一个页面可以下载文本文件。在页面中,我首先将文本文件解密为string plainText
。然后将该字符串写入文件并上传到名为"解密文件"的文件夹中。下载解密的文件并删除以前保存的文件。
这是我下载的代码
//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页面没有完成加载。我的代码出了什么问题?
现在我知道我的代码有什么问题。在结束之前,我删除了这个文件。所以我按如下方式更改了代码,一切都很好。
//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));
Response.Flush();
//Delete File from the folder
if (System.IO.File.Exists(Server.MapPath("~/Decrypted Files/" + FileName)))
{
System.IO.File.Delete((Server.MapPath("~/Decrypted Files/" + FileName)));
}
HttpContext.Current.Response.End();
将该按钮(用于导出)放入更新面板