3

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

Fasco
  • 233
  • 3
  • 18
  • 1
    Instead of creating a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) from the data, create a [Blob Object](https://developer.mozilla.org/en-US/docs/Web/API/Blob). This will avoid the 33% extra overhead of base64 encoding. – georgeawg Mar 27 '18 at 11:04
  • Example here with Blob Object https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link/19328891#19328891 – GramThanos Mar 27 '18 at 12:18
  • Thank you for your answers but it doesn't work with a large base64 (length more than 3472174 characters) – Fasco Mar 27 '18 at 12:28

1 Answers1

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();
Antoine Galluet
  • 320
  • 4
  • 14
Fasco
  • 233
  • 3
  • 18