Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert base64 to downloadable zip file in MVC c#

I have been able to download a zipfile before but then the compression happend on the ASP server. Now we have changed this action to another server (Progress).

At this moment I'm receiving a base64 encoded string which represents a zip file. But how can I convert this string to a zipfile. The code I used before you can find beneath, can I reuse code?

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

I have no idea how to convert a string to a zip file. Thanks in advance for your help

like image 240
thomvlau Avatar asked Dec 09 '25 23:12

thomvlau


1 Answers

Convert the base64 to a byte array by doing:

Convert.fromBase64String(strBase64);

Then I found an article to easily download the zipfile

Download file of any type in Asp.Net MVC using FileResult?

This article suggests:

public FileResult Download()
{
    string base64 = getBase64ZIP();
    byte[] byteArray = Convert.fromBase64String(base64);
    return File(byteArray, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
like image 192
thomvlau Avatar answered Dec 12 '25 12:12

thomvlau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!