如何在MVC中以Zip格式下载多个文件

时间:2019-02-11 07:39:00

标签: c# asp.net-mvc zipfile

我有一些特定用户的文件,当单击全部下载按钮时,必须下载。我可以将结果从过程存储到 objResult 中。我需要下载用户的多个文件作为压缩文件

以下是单次下载的代码:

public ActionResult DownloadParticularFile(Int64 NurseId, Int64 PostedJobId, Int64 DocumentId)
    {
        NurseDAL objNurseDAL = new NurseDAL();

        Result objResult = objNurseDAL.FetchDocumentURLfromDocID(DocumentId);
        string path = "D:/TFSProjects/Dot Net Project/NurseOneStop.WebSite/NurseOneStop.WebSite/";
        byte[] fileBytes = System.IO.File.ReadAllBytes(path + objResult.Results.DocumentUrl);
        var URL = objResult.Results.DocumentUrl;

        string fileName = Path.GetFileName(URL);
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }

这就是我使用“全部下载”按钮的方式:

public ActionResult DownloadAll(Int64 NurseId, Int64 PostedJobId)
    {
        NurseDAL objNurseDAL = new NurseDAL();

        Result objResult = objNurseDAL.FetchDocumentListforNurse(NurseId, PostedJobId);

        using (var zipStream = new ZipOutputStream(Response.OutputStream))
        {
            foreach(string filePath in objResult.Results)
            {
                string path = "D:/TFSProjects/Dot Net Project/NurseOneStop.WebSite/NurseOneStop.WebSite/";
                byte[] fileBytes = System.IO.File.ReadAllBytes(path + objResult.Results.DocumentUrl);
                var URL = objResult.Results.DocumentUrl;
                string fileName = Path.GetFileName(URL);
                zipStream.PutNextEntry(fileName);
                zipStream.Write(fileBytes, 0, fileBytes.Length);
            }
            zipStream.Flush();
            zipStream.Close();
        }
            return null;
    }

我需要将文件下载为Zip文件。

附言:我不知道如何使用Zip。

0 个答案:

没有答案