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();
Run Code Online (Sandbox Code Playgroud)
实际的zip文件c:\ temp\test.zip是好的,有效的,无论你想要什么.当我导航到目录c:\ temp \并双击test.zip文件时; 它打开了.
我的问题似乎只与下载有关.上面的代码执行没有任何问题.提供了文件下载对话框.我可以选择保存或打开.如果我尝试从对话框中打开文件,或保存它然后打开它.我收到以下对话框消息:
压缩(压缩)文件夹无效或已损坏.
对于Response.ContentType我尝试过:
application/x-compressed application/x-zip-compressed application/x-gzip-compresse application/octet-stream application/zip
正在使用以下代码创建zip文件(我确信由于我能够直接打开创建的文件而正常工作):Ionic.zip