从网址链接压缩多个pdf,如何

时间:2019-05-01 00:50:08

标签: c# pdf asp.net-core zip memorystream

我有一个项目,要求从URL链接压缩pdf文件,然后由最终用户浏览器下载并单击。到目前为止,我已经能够压缩一个pdf文件,而这几乎不是我想要的。

我不确定如何从这里继续。下面是代码。任何帮助将不胜感激。

  

ASP.NET Core


        [HttpGet("zipFiles")]
        public IActionResult ZipPDFFiles()
        {
            var fileNames = _repo.GetFileNames();

            foreach (var filesName in fileNames)
            {
                var urlLink = "https://example.com/folder/" + $"{filesName.PdfFileName}";

                var net = new System.Net.WebClient();
                var data = net.DownloadData(urlLink);

                var file = $"{filesName.PdfFileName}";

                var contentType = "application/zip";

                string zippedFolderName = "Archive.zip";

                using (MemoryStream ms = new MemoryStream())
                {
                    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                    {
                        var zipArchiveEntry = archive.CreateEntry($"{file}", System.IO.Compression.CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(data, 0, data.Length);
                    }
                    return File(ms.ToArray(), contentType, $"{zippedFolderName}");
                }

            }
            return NotFound();
        }

1 个答案:

答案 0 :(得分:0)

一些改进:

  • 使用的HttpClient
  • 文件同时上传
class ClassName(BaseClass):
    def __init__(self, args): # this is the constructor