2

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

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
asd
  • 65
  • 2
  • 3
  • 8

8 Answers8

5

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();
joshuapoehls
  • 32,695
  • 11
  • 50
  • 61
  • 1
    You should specify the invariant culture in the call to ToString(). (`updateFile.Length.ToString(CultureInfo.InvariantCulture)` – erikkallen Feb 04 '11 at 14:27
1

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

cjk
  • 45,739
  • 9
  • 81
  • 112
0

I have tested the flowing code and it works, so sharing it. One mandatory thing that I did is to use "Response.Flush();".

private void DownloadCandidateDocument()
{
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    var buffer = new byte[] {};

    buffer = get you byte array here from service or file system

    if (buffer.Length > 0)
    {
        Response.ContentType = "application/zip";
        Response.BinaryWrite(buffer);

        var fileName = "myzipfile";
        Response.AddHeader("content-disposition",
            string.Format(@"attachment;filename=""{0}""", fileName));

        Response.Flush();    
    }
}
Shyam Bhimani
  • 1,310
  • 1
  • 22
  • 37
Masudur
  • 56
  • 3
0

Try Response.WriteFile(sZipFilePath); instead

Steve B
  • 36,818
  • 21
  • 101
  • 174
0

Try:

HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + "test.zip");
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.BinaryWrite(ba);
HttpContext.Current.Response.End();

Where ba is your byte array representing the zip file

alexl
  • 6,841
  • 3
  • 24
  • 29
  • but that means loading the file into memory, which won't scale as well as just streaming it from disk – Rup Feb 04 '11 at 13:52
0

I'm using this:

Response.ContentType = "application/zip" ;
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(path)));
Response.WriteFile(path);
Response.End();

where the variable path is the full path of the (local) file

riffnl
  • 3,248
  • 1
  • 19
  • 32
0

are you zipping the file on the server, or downloading files already zipped? i think there will be something wrong in returning the file that are zipped, if you are using jscript. if not then you are not properly returning the files name that being zipped.

i have also done like this, make zipped file and then download it. i used c#, jscript and asp.net.

and it worked perfect for me. i pass the file/files to a jscript file, and jscript call a method to zipped the files, when zipping is being completed on server, the server return it to the client and start download automatically.

safi
  • 3,636
  • 8
  • 24
  • 37
0

I don't know what machine you are using ... but windows releases before Vista use the FAt32 encoding for zip files, with Vista Microsft started to use NTFS as a file format. Please review your archive file format because I've faced a very similar situation when the user had a XP OS and I was creating the archive in win7 (the error message was the same you've posted). Best regards,

  • 1
    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. – Rup Sep 13 '11 at 11:12