想要替代Response.End()
本文关键字:End Response | 更新日期: 2023-09-27 18:22:46
我尝试在下载文件后删除文件。我的代码是
private void DownloadZipFileDialogue(string strZipFilePath)
{
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strZipFilePath));
Response.TransmitFile(strZipFilePath);
Response.End();
//File.Delete(strZipFilePath);
}
我知道Response.End();
之后没有代码块被执行。如果我试图在Response.End();
zip文件损坏之前删除文件。我到处找。我尝试:ApplicationInstance.CompleteRequest();
代替Response.End();
但我得到了相同的结果。zip文件已损坏。我看到这是Response.End()被认为是有害的吗?但无法找到解决方案。任何解决问题的想法。谢谢
你可以试试这个,
private void DownloadZipFileDialogue(string strZipFilePath)
{
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strZipFilePath));
using(Stream input = File.OpenRead(strZipFilePath)){
/// .NET 4.0, use following line if its .NET 4 project
input.CopyTo(Response.OutputStream);
/// .NET 2.0, use following lines if its .NET 2 project
byte[] buffer = new byte[4096];
int count = input.Read(buffer,0,buffer.Length);
while(count > 0){
Response.OutputStream.Write(buffer,0,count);
count = input.Read(buffer,0,buffer.Length);
}
}
File.Delete(strZipFilePath);
}