在 MVC C# 中将 base64 转换为可下载的 zip 文件

tho*_*lau 3 c# asp.net-mvc zip base64

我之前能够下载 zip 文件,但压缩发生在 ASP 服务器上。现在我们已将此操作更改为另一台服务器(进度)。

此时我收到一个表示 zip 文件的 Base64 编码字符串。但我怎样才能将这个字符串转换为 zip 文件。我之前使用的代码可以在下面找到,我可以重复使用代码吗?

MemoryStream outputStream = new MemoryStream();
outputStream.Seek(0, SeekOrigin.Begin);

using (ZipFile zip = new ZipFile())
{
   foreach (string id in idArray)
   {
        string json = rest.getDocumentInvoice(Convert.ToInt32(id));
        byte[] file = json.convertJsonToFile();
        zip.AddEntry("invoice" + id + ".pdf", file);
   }
    zip.Save(outputStream);
}


outputStream.WriteTo(Response.OutputStream);
Response.AppendHeader("content-disposition", "attachment; filename=invoices.zip");
Response.ContentType = "application/zip";
return new FileStreamResult(outputStream, "application/zip");
Run Code Online (Sandbox Code Playgroud)

我不知道如何将字符串转换为 zip 文件。在此先感谢您的帮助

tho*_*lau 5

通过执行以下操作将 base64 转换为字节数组:

Convert.fromBase64String(strBase64);
Run Code Online (Sandbox Code Playgroud)

然后我找到一篇文章可以轻松下载zip文件

使用 FileResult 在 Asp.Net MVC 中下载任何类型的文件?

本文建议:

public FileResult Download()
{
    string base64 = getBase64ZIP();
    byte[] byteArray = Convert.fromBase64String(base64);
    return File(byteArray, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
Run Code Online (Sandbox Code Playgroud)