Asp.net mvc 在MVC中使用memorystream和DotNetZip可以;无法访问已关闭的流";

Asp.net mvc 在MVC中使用memorystream和DotNetZip可以;无法访问已关闭的流";,asp.net-mvc,dotnetzip,Asp.net Mvc,Dotnetzip,我正在尝试使用DotNetZip组件在MVC方法中创建zipfile 这是我的密码: public FileResult DownloadImagefilesAsZip() { using (var memoryStream = new MemoryStream()) { using (var zip = new ZipFile()) { zip.AddDirector

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

当我运行它时,我得到一个“无法访问封闭流”错误,我不知道为什么。

不要处理
内存流
,一旦
文件流结果
完成写入响应,它就会处理好:

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");
}
无法发表评论

达林的回答很好!仍然收到内存异常,但必须添加
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())
        {
...

以防万一不明显:)

太好了!非常感谢:)我不得不添加
memoryStream.Seek(0,SeekOrigin.Begin)之前<代码>返回文件(…)以使其工作。