ASP.NET创建zip文件进行下载:压缩的压缩文件夹无效或损坏 [英] ASP.NET Create zip file for download: the compressed zipped folder is invalid or corrupted

查看:670
本文介绍了ASP.NET创建zip文件进行下载:压缩的压缩文件夹无效或损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

实际的zip文件c:\temp\test.zip是好的,有效的,无论你想叫它当我导航到目录c:\temp\并双击test.zip文件;它直接打开。

The actual zip file c:\temp\test.zip is good, valid, whatever you want to call it. When I navigate to the directory c:\temp\ and double-click on the test.zip file; it opens right up.

我的问题似乎只是下载。上面的代码执行没有任何问题。介绍文件下载对话框。我可以选择保存或打开。如果我尝试从对话框打开文件,或者保存它然后打开它。我收到以下对话框消息:

My problem seems only to be with the download. The code above executes without any issue. A file download dialog is presented. I can chose to either save or open. If I try to open the file from the dialog, or save it and then open it. I get the following dialog message:

压缩(压缩)文件夹无效或损坏。

The Compressed (zipped) Folder is invalid or corrupted.

.ContentType我试过:

For Response.ContentType I've tried:

应用程序/ x压缩
应用程序/ x-zip压缩
应用程序/ x-gzip-compresse
应用程序/八位字节流
应用程序/ zip

application/x-compressed application/x-zip-compressed application/x-gzip-compresse application/octet-stream application/zip

正在使用一些先前的代码创建zip文件(我确定正在工作正常以我的能力直接打开创建的文件)使用:Ionic.zip

The zip file is being created with some prior code (that I'm sure is working fine due to my ability to open the created file directly) using: Ionic.zip

http://www.codeplex.com/DotNetZip

推荐答案

这个工作。我不知道为什么这样做。

This worked. I don't know why but it did.

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

这篇关于ASP.NET创建zip文件进行下载:压缩的压缩文件夹无效或损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆