C# 如何创建zip并将其存储在链接MVC中,或向用户提供zip并重定向

C# 如何创建zip并将其存储在链接MVC中,或向用户提供zip并重定向,c#,asp.net-mvc,file,model-view-controller,download,C#,Asp.net Mvc,File,Model View Controller,Download,我能够毫无问题地创建一个zip,这是我唯一不能做的事情,将zip文件存储在一个链接中,这样当用户单击链接时,它就会下载该文件 Response.Clear(); Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "filename=Photo.zip"); using (ZipFile zip = new ZipFile()) { for

我能够毫无问题地创建一个zip,这是我唯一不能做的事情,将zip文件存储在一个链接中,这样当用户单击链接时,它就会下载该文件

Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=Photo.zip");

using (ZipFile zip = new ZipFile())
        {
            foreach (var pictures in pictureList)
            {
                zip.AddFile(Server.MapPath("~\\Content\\pictures\\upload\\" + pictures.name),"images");
            }
            zip.Save(Response.OutputStream);
        }
Response.End();

下面的代码用于文件下载

public void DownloadFile(string fileName)
 {
    FileInfo file = new FileInfo(@"D:\DOCS\"+fileName);
    Context.Response.Clear();
    Context.Response.ClearHeaders();
    Context.Response.ClearContent();
    Context.Response.AddHeader("Content-Disposition", "attachment;  filename=" + file.Name); Context.Response.AddHeader("Content-Length", file.Length.ToString());
    Context.Response.ContentType = "application/zip"; 
    Context.Response.Flush();
    Context.Response.TransmitFile(file.FullName);
    Context.Response.End();
}

但是,在Response.End()之后调用Response.Redirect将不起作用。如果您真的想重定向页面,您可能需要考虑另一种方法。

下面的代码适用于文件下载

public void DownloadFile(string fileName)
 {
    FileInfo file = new FileInfo(@"D:\DOCS\"+fileName);
    Context.Response.Clear();
    Context.Response.ClearHeaders();
    Context.Response.ClearContent();
    Context.Response.AddHeader("Content-Disposition", "attachment;  filename=" + file.Name); Context.Response.AddHeader("Content-Length", file.Length.ToString());
    Context.Response.ContentType = "application/zip"; 
    Context.Response.Flush();
    Context.Response.TransmitFile(file.FullName);
    Context.Response.End();
}

但是,在Response.End()之后调用Response.Redirect将不起作用。如果您真的想重定向页面,您可能必须考虑另一种方法。

为什么不?你试了什么?有很多方法可以做到这一点;Response.ContentType=“应用程序/zip”;AddHeader(“内容处置”,“文件名=Photo.zip”);然后是Response.End(),但问题是它没有重定向我的页面,所以我被困在一个我不想要的页面上。如果有办法向用户提供zip文件并同时重定向页面,它也会解决我的问题,然后是Response.End()-你真的流式传输了文件吗?其他部分是正确的。您还可以将
zip
存储在您的应用程序可以访问的地方,然后提供一个逐字链接:
www.mywebsite.com/zipfiles/myzip.zip
并将您的zip存储在虚拟目录的文件夹
zipfiles
中。响应之后,我什么都没有。结束,您能举一个例子说明您要解释的内容吗我?为什么不?你试了什么?有很多方法可以做到这一点;Response.ContentType=“应用程序/zip”;AddHeader(“内容处置”,“文件名=Photo.zip”);然后是Response.End(),但问题是它没有重定向我的页面,所以我被困在一个我不想要的页面上。如果有办法向用户提供zip文件并同时重定向页面,它也会解决我的问题,然后是Response.End()-你真的流式传输了文件吗?其他部分是正确的。您还可以将
zip
存储在您的应用程序可以访问的地方,然后提供一个逐字链接:
www.mywebsite.com/zipfiles/myzip.zip
,并将您的zip存储在虚拟目录的文件夹
zipfiles
中。响应之后,我什么都没有。结束,您能举一个例子说明一下您向我解释的内容吗?