【问题标题】:Using memorystream and DotNetZip in MVC gives "Cannot access a closed Stream"在 MVC 中使用 memorystream 和 DotNetZip 会给出“无法访问关闭的流”
【发布时间】:2012-10-16 16:58:38
【问题描述】:

我正在尝试使用 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");
            }
        }
    }

当我运行它时,我收到“无法访问已关闭的流”错误,我不知道为什么。

【问题讨论】:

    标签: asp.net-mvc dotnetzip


    【解决方案1】:

    不要丢弃MemoryStreamFileStreamResult 将在完成将其写入响应后处理:

    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");
    }
    

    【讨论】:

    • 完美!非常感谢:)
    • 我必须在 return File(...); 之前添加 memoryStream.Seek(0, SeekOrigin.Begin); 才能让它工作。
    【解决方案2】:

    无法评论。

    达林的回答很棒!仍然收到内存异常,但必须添加response.BufferOutput = false;并且因此不得不将内容处置代码移到更高的位置。

    所以你有:

    ...
            var response = context.HttpContext.Response;
            response.ContentType = "application/zip";
            response.BufferOutput = false;
    
            var cd = new ContentDisposition
            {
                FileName = ZipFilename,
                Inline = false
            };
            response.Headers.Add("Content-Disposition", cd.ToString());
    
            using (var zip = new ZipFile())
            {
    ...
    

    以防万一不是很明显:)

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 2012-06-11
      • 2012-03-19
      • 2020-08-17
      • 2018-10-14
      • 2017-08-09
      • 2011-01-20
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多