亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 ZipArchive 與 ASP.NET Core Web Api 結合使用

將 ZipArchive 與 ASP.NET Core Web Api 結合使用

C#
喵喔喔 2021-06-30 13:04:43
問題是如何使用 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 回答

  • 1 回答
  • 0 關注
  • 312 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號