2

I try to delete file after download file.My code is

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);
 }

I know after Response.End(); no code block is execute.If i try to delete file befor Response.End(); zip file is corrupted.I search every where.I try:
ApplicationInstance.CompleteRequest();
insteadof
Response.End();.
But i get same result.zip file is corrupted.I see this Is Response.End() considered harmful? but unable to find solution.Any idea to solve the problem.Thanks.

Community
  • 1
  • 1
4b0
  • 21,981
  • 30
  • 95
  • 142

1 Answers1

1

You can try this,

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);
}
Akash Kava
  • 39,066
  • 20
  • 121
  • 167