2

我之前可以下载一个 zipfile,但是压缩发生在 ASP 服务器上。现在我们已将此操作更改为另一台服务器(进度)。

此时我收到一个代表 zip 文件的 base64 编码字符串。但是如何将此字符串转换为 zipfile。我之前使用的代码可以在下面找到,我可以重复使用代码吗?

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");

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

4

3 回答 3

4

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

Convert.fromBase64String(strBase64);

然后找了一篇文章轻松下载zipfile

使用 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);
}
于 2013-10-22T10:17:58.183 回答
0

用于Convert.FromBase64String获取 zip 文件的字节数。

string base64String = rest.getDocumentInvoice(Convert.ToInt32(id));
byte[] file = Convert.FromBase64String(base64String);
using(var stream = new MemoryStream(file))
{
    zip.AddEntry("invoice" + id + ".pdf", stream);
}
于 2013-10-21T15:27:56.070 回答
0

尝试这个:

HTML:

>  <a id='dwnldLnk' download='myzip.zip' style="display:none;" /> 
    >     <a ng-click="saveFile()">myzip.zip</a>

控制器:

var dataBase64 = "UEsDBAoAAAAAAORSo04AAAAAAAAAAAAAAAAEABwAc3VtL1VUCQADG7TLXKu/y1x1eAsAAQToAwAABOgDAABQSwMEFAAAAAgAe3WTTkQjafoxAAAAMgAAAAoAHABzdW0vc3VtLmdvVVQJAAM6fL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gLAAEE6AMAAAToAwAAUEsBAh4DFAAAAAgAe3WTTkQjafoxAAAAMgAAAAoAGAAAAAAAAQAAAP+BPgAAAHN1bS9zdW0uZ29VVAUAAzp8uVx1eAsAAQToAwAABOgDAABQSwECHgMUAAAACAC2SpZOSyDHc2cCAAA+BAAABwAYAAAAAAABAAAA/4GzAAAAbWFpbi5nb1VUBQADOCW9XHV4CwABBOgDAAAE6AMAAFBLBQYAAAAAAwADAOcAAABbAwAAAAA="
var dataFile = 'data:application/zip;base64,' + dataBase64 
  var dlnk = document.getElementById('dwnldLnk');
  dlnk.href = dataFile;
  dlnk.click();
于 2019-05-04T09:28:14.777 回答