最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - Download a CSV file in Javascript (or Symfony) from a Base64 - Stack Overflow

programmeradmin10浏览0评论

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();
发布评论

评论列表(0)

  1. 暂无评论