簡體   English   中英

想要Response.End()的替代方法

[英]want alternative of Response.End()

下載文件后我嘗試刪除文件。我的代碼是

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();
但是我得到相同的result.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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM