Fro*_*rud 5 asp.net-mvc dotnetzip
我正在尝试使用DotNetZip组件在MVC方法中创建一个zipfile.
这是我的代码:
    public FileResult DownloadImagefilesAsZip()
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var zip = new ZipFile())
            {
                zip.AddDirectory(Server.MapPath("/Images/"));
                zip.Save(memoryStream);
                return File(memoryStream, "gzip", "images.zip");
            }
        }
    }
当我运行它时,我得到一个"无法访问关闭的流"错误,我不知道为什么.
Dar*_*rov 15
不要丢弃MemoryStream,FileStreamResult一旦完成将其写入响应,将会小心:
public ActionResult DownloadImagefilesAsZip()
{
    var memoryStream = new MemoryStream();
    using (var zip = new ZipFile())
    {
        zip.AddDirectory(Server.MapPath("~/Images"));
        zip.Save(memoryStream);
        return File(memoryStream, "application/gzip", "images.zip");
    }
}
顺便说一下,我建议你编写一个自定义操作结果来处理这个,而不是在你的控制器动作中编写管道代码.不仅如此,您将获得可重用的操作结果,但请记住,您的代码效率非常低= =您正在内存中执行ZIP操作,从而将整个〜/ images目录内容+ zip文件加载到内存中.如果此目录中有许多用户和大量文件,您将很快耗尽内存.
更有效的解决方案是直接写入响应流:
public class ZipResult : ActionResult
{
    public string Path { get; private set; }
    public string Filename { get; private set; }
    public ZipResult(string path, string filename)
    {
        Path = path;
        Filename = filename;
    }
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        var response = context.HttpContext.Response;
        response.ContentType = "application/gzip";
        using (var zip = new ZipFile())
        {
            zip.AddDirectory(Path);
            zip.Save(response.OutputStream);
            var cd = new ContentDisposition
            {
                FileName = Filename,
                Inline = false
            };
            response.Headers.Add("Content-Disposition", cd.ToString());
        }
    }
}
然后:
public ActionResult DownloadImagefilesAsZip()
{
    return new ZipResult(Server.MapPath("~/Images"), "images.zip");
}
| 归档时间: | 
 | 
| 查看次数: | 5171 次 | 
| 最近记录: |