javascriptangularjsexport-to-csvhtml5-filesystem

Unable to save content more than 8Mb in a CSV file with Angular JS


I get a string as response from my backend with more than 8MB of csv content.

with this code :

myService.getCsvExportContent(vm.searchParams).then(function (content) {
        var encodedUri = encodeURI(content);
        var link = document.createElement("a");
        link.setAttribute("href", encodedUri);
        link.setAttribute("download", "MCsExport.csv");
        document.body.appendChild(link);
        link.click();
      });

When the result is less that 8Mb, it creates the whole file but when it's more than 8Mb, it saves the half of data to the file and I lose data. (I can see the size by debugging in my browser)

I tried angular-file-saver but I still got errors the injection of the dependency doesn't work for me in AngularJS.

I tried creating Blob object with the content, same result.

Please help. Thank you.


Solution

  • Try this way, that works for me:

    myService.getCsvExportContent(vm.searchParams).then(function (content) {
        var link = document.createElement("a");
        var blob = new Blob([content],{type: 'text/csv;charset=utf-8;'});
        var url = URL.createObjectURL(blob);
        link.href = url;
        link.setAttribute("download", "MCsExport.csv");
        link.click();
      });