admin管理员组

文章数量:1327945

I'm using a method to download a CSV file from a Base64.

var dlnk = document.getElementById("myDownloadButton");
dlnk.href = 'data:application/octet-stream;base64,' + myBase64;
dlnk.click();

It worked until I realized that when the base64 is too long, I have a problem. It will open a white page and it won't download my CSV file. I think it's because my base64 is too long.

Do you have an alternative in JavaScript or in Symfony 3?

Thank you

I'm using a method to download a CSV file from a Base64.

var dlnk = document.getElementById("myDownloadButton");
dlnk.href = 'data:application/octet-stream;base64,' + myBase64;
dlnk.click();

It worked until I realized that when the base64 is too long, I have a problem. It will open a white page and it won't download my CSV file. I think it's because my base64 is too long.

Do you have an alternative in JavaScript or in Symfony 3?

Thank you

Share Improve this question asked Mar 27, 2018 at 10:32 FascoFasco 2431 gold badge3 silver badges18 bronze badges 3
  • 1 Instead of creating a data URL from the data, create a Blob Object. This will avoid the 33% extra overhead of base64 encoding. – georgeawg Commented Mar 27, 2018 at 11:04
  • Example here with Blob Object stackoverflow./questions/19327749/… – GramThanos Commented Mar 27, 2018 at 12:18
  • Thank you for your answers but it doesn't work with a large base64 (length more than 3472174 characters) – Fasco Commented Mar 27, 2018 at 12:28
Add a ment  | 

1 Answer 1

Reset to default 7

The solution:

let csvContent = atob("YOUR BASE64");
var blob = new Blob([csvContent], {type: "data:application/octet-stream;base64"});
var url  = window.URL.createObjectURL(blob);
// you need to precise a front-end button to have a name
var dlnk = document.getElementById(nameFile);
dlnk.href = url;
dlnk.click();

本文标签: angularjsDownload a CSV file in Javascript (or Symfony) from a Base64Stack Overflow