我有一个函数,用于压缩用户 session 中的文件列表,然后将其流式传输到用户的浏览器以供下载:
public static void DownloadAllPhotos()
{
HttpContext.Current.Response.AddHeader(
"Content-Disposition", "attachment; filename=Photos.zip");
HttpContext.Current.Response.ContentType = "application/zip";
List<string> photos= new List<string>();
if (HttpContext.Current.Session != null &&
HttpContext.Current.Session["userPhotos"] != null)
{
photos = (List<string>)HttpContext.Current.Session["userPhotos"];
}
using (var zipStream = new
ZipOutputStream(HttpContext.Current.Response.OutputStream))
{
foreach (string photoUrl in photos)
{
byte[] fileBytes = File.ReadAllBytes(photoUrl);
var fileEntry = new ZipEntry(
Path.GetFileName(photoUrl))
{
Size = fileBytes.Length
};
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
zipStream.Flush();
zipStream.Close();
// reset session
HttpContext.Current.Session["userPhotos"] = new List<string>();
}
}
当用户在其 session 中具有照片网址并单击按钮来调用此函数时,文件将被压缩并在用户的浏览器中开始下载。
但是当我尝试打开压缩文件时,出现此错误:
Windows cannot open the folder.
The compressed folder "{Path to my file}" is invalid.
我是否做错了什么导致了这个错误?
最佳答案
查看 this example 中 Response.Flush
和 ZipEntry.CleanName
的位置并看看编写类似的内容是否可以解决问题。
关于c# - 使用 SharpZipLib 压缩文件时出现 "Invalid folder"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376519/