how to transmit a zip file from c# code

16,491

Solution 1

I'm not sure exactly why your snippet isn't working but here is a bit of code I'm using to do the same thing in my application. Hope it helps.

var updateFile = new FileInfo("path/to/file");
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(updateFile.FullName) + "\"");
Response.AddHeader("content-length", updateFile.Length.ToString());
Response.TransmitFile(updateFile.FullName);
Response.Flush();

Solution 2

Try adding Response.End() on the end of your script.

Share:
16,491
asd
Author by

asd

Updated on June 04, 2022

Comments

  • asd
    asd almost 2 years

    I am using c#.net application in which I need to download a zip file using c# codebase.I am using the following code for downloading the file:

    Response.ContentType = "application/zip"                       
    Response.AppendHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(sZipFileName)));
    Response.TransmitFile(sZipFilePath);
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    

    The zip file is transmitted but when I tried to open the zip file after downloading, I am getting an error saying " Cannot open the file : the file does not look to be a valid archive"

    Please let me know where am I doing wrong and how to get a zip file and extract it without any errors.

    Thanks in Advance

  • asd
    asd about 13 years
    When I add this, I am getting an exception saying "ThreabAborted exception".
  • Rup
    Rup about 13 years
    but that means loading the file into memory, which won't scale as well as just streaming it from disk
  • erikkallen
    erikkallen about 13 years
    You should specify the invariant culture in the call to ToString(). (updateFile.Length.ToString(CultureInfo.InvariantCulture)
  • Rup
    Rup over 12 years
    I don't think that would matter. The .zip is independent of the filesystem that it was created on: the only possible difference between a zip created from FAT32 and from NTFS is that you could add the NTFS file permission data in as metadata, in the same way that Unix permissions get stored. But these shouldn't interfere with non-NTFS systems.