簡體   English   中英

如何創建zip並將其存儲在鏈接MVC中或向用戶提供zip並重定向

[英]how to create zip and stock it in a link MVC or provide zip to user and redirect

我能夠毫無問題地創建一個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將無法工作。 如果您確實要重定向頁面,則可能不得不考慮另一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM