how to transmit a zip file from c# code

18.1k views Asked by At

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

8

There are 8 answers

0
Masudur On

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();    
    }
}
0
Steve B On

Try Response.WriteFile(sZipFilePath); instead

1
joshuapoehls On

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();
1
alexl On

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

0
riffnl On

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

1
cjk On

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

0
safi On

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.

1
Luis Gonzaga On

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,