ASP.NET Create a zip file for download: compressed compressed folder is invalid or corrupt

string fileName = "test.zip"; string path = "c:\\temp\\"; string fullPath = path + fileName; FileInfo file = new FileInfo(fullPath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.Buffer = true; Response.AppendHeader("content-disposition", "attachment; filename=" + fileName); Response.AppendHeader("content-length", file.Length.ToString()); Response.ContentType = "application/x-compressed"; Response.TransmitFile(fullPath); Response.Flush(); Response.End(); 

The actual zip file c: \ temp \ test.zip is good, valid, no matter what you want to name. When I go to the c: \ temp \ directory and double-click the test.zip file; he opens up.

My problem seems to be related only to the download. The above code is no problem. The file upload dialog is presented. I can choose to save or open. If I try to open a file from a dialog or save it, then open. I get the following dialog box:

A compressed (encoded) folder is invalid or damaged.

For Response.ContentType I tried:

application / x-compressed application / x-zipper compressed application / x-GZIP-compresse Application / octet stream Application / mail

A zip file is created with some previous code (I'm sure it works fine due to my ability to directly open the created file) with: Ionic.zip

http://www.codeplex.com/DotNetZip

+4
source share
1 answer

It worked. I do not know why, but it happened.

 string fileName = "test.zip"; string path = "c:\\temp\\"; string fullPath = path + fileName; FileInfo file = new FileInfo(fullPath); Response.Clear(); //Response.ClearContent(); //Response.ClearHeaders(); //Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); //Response.AppendHeader("Content-Cength", file.Length.ToString()); Response.ContentType = "application/x-zip-compressed"; Response.WriteFile(fullPath); //Response.Flush(); Response.End(); 
+21
source

Source: https://habr.com/ru/post/1304293/


All Articles