我尝试在下载文件后删除文件。我的代码是
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();
之后没有代码块执行。如果我尝试删除文件befor Response.End();
zip文件已损坏。我搜索每个地方。我尝试:
{{1} }
与其使用
ApplicationInstance.CompleteRequest();
。
但我得到相同的result.zip文件已损坏。我看到这个Is Response.End() considered harmful?但无法找到解决方案。任何解决问题的想法。谢谢。
答案 0 :(得分:1)
你可以试试这个,
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);
}