問題是如何使用 ASP.NET Core 2(當前)Web API 動態創建壓縮(壓縮)文件夾?我在用 System.IO.Compression.ZipArchive我已經有幾篇博客文章使用流或字節數組來完成,所有這些都給了我相同的輸出。我可以下載 zip 文件夾,但無法打開它。壓縮后的文件夾大小正確。雖然打不開。我希望它工作的方式是讓用戶單擊運行此操作的按鈕并返回一個包含一個或多個文件的壓縮文件夾。[HttpGet][Route("/api/download/zip")]public async Task<IActionResult> Zip(){ byte[] bytes = null; using (MemoryStream zipStream = new MemoryStream()) using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) { var tempFileName = await _azure.GetFilesByRef("Azure_FilePath"); // Running just this line gives me the zipped folder (empty) which I can open ZipArchiveEntry entry = zip.CreateEntry("File1.pdf", CompressionLevel.Fastest); // Adding this 2nd section will download the zip but will not open the zip folder using (Stream stream = entry.Open()) using (FileStream fs = new FileStream(tempFileName, FileMode.Open, FileAccess.Read)) { await fs.CopyToAsync(stream); } bytes = zipStream.ToArray(); } return File(bytes, MediaTypeNames.Application.Zip, $"Attachments{DateTime.Now.ToBinary()}.zip");}任何人都可以發現錯誤或提出替代解決方案嗎?
- 1 回答
- 0 關注
- 312 瀏覽
添加回答
舉報
0/150
提交
取消