how to transmit a zip file from c# code

asd picture asd · Feb 4, 2011 · Viewed 16.6k times · Source

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

Answer

joshuapoehls picture joshuapoehls · Feb 4, 2011

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