问题描述
在我的asp中.net网页,我有一个网格和下载按钮。网格从共享位置填充有关文件的信息,
下载按钮在压缩后下载所有文件。我正在使用Ionic.Zip库进行压缩。以下是用户单击下载按钮时执行的代码。在将文件信息(如名称
等)绑定到GridView控件之前,问题是下载工作正常,但在将文件信息绑定到GridView控件后中断。我收到错误"压缩的zip文件夹无效"但是当我使用Ionic.Zip库保存zip文件时它没有损坏,我可以手动打开
!
HttpContext.Current.Response.Clear();
Response.ClearContent();Response.Clear();string LogPath = ConfigurationManager.AppSettings["LogPath"];
LogPath = Path.Combine(LogPath, Application);
//Zip File will be created when one of the file is in use during Zip.
ZipFile Zip = new ZipFile();
Zip.AddDirectory(LogPath);
string _DateTime = System.DateTime.Now.ToString("ddMMyyHHmmssfff");
string ZipFileName = string.Format("ZipFile{0}.zip",_DateTime);
//Zip.Save(Response.OutputStream);
ErrorMessage.Text = Response.OutputStream.CanRead + " " + Response.OutputStream.CanWrite;
string _Path = MapPath(".");
_Path = Path.Combine(_Path, ZipFileName);
ErrorMessage.Visible = true;
Zip.Save(_Path);
// Response.AppendHeader("content-disposition", "filename=" + ZipFileName);
Response.AppendHeader("content-disposition",
"attachment; filename=" + ZipFileName);
//Response.ContentType = "application/zip";
Response.ContentType = "application/unknown";
Response.WriteFile(_Path);
Kaleem。
推荐答案
从ASP下载二进制数据的预期方法.Net服务器通过自定义HTTP处理程序:
http://www.codeproject.com/Articles/544289/Restricting-files-download-using-HTTP-Handler-base
The intended way to download binary data from a ASP.Net server is via a custom HTTP Handler:
http://www.codeproject.com/Articles/544289/Restricting-files-download-using-HTTP-Handler-base
从.NET 4.5开始我们有一个用于制作和使用zip文件的类(甚至没有解压缩):
HTTPS://msdn.mi crosoft.com/en-us/library/system.io.compression.ziparchive.aspx
Starting with .NET 4.5 we got a build in class for making and working with zip files (even without unpacking):
https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx
缓存某些常见的文件组合可能是值得的。但一般来说,写一个磁盘是完全有问题的。
It might be worthwhile to cache certain common combinations of files. But generally speaking writing the file do a disk is entirely unessesary.
这篇关于Web下载文件已损坏asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!