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

本文关键字:zip 或者 MVC 重定向 用户 链接 存储 何创建 创建 | 更新日期: 2023-09-27 18:26:53

我可以毫无问题地创建一个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();

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

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

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.RRedirect将不起作用。如果你真的想重定向页面,你可能必须考虑另一种方式。