Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular save file as csv result in Failed- network error Only on Chrome

I'm using the following code to save file as csv.

$scope.saveCSVFile = function (result)
{
    var a         = document.createElement('a');
    a.href = 'data:application/csv;charset=utf-8,' + encodeURIComponent(result.data);
    a.target      = '_blank';
    a.download    =  $scope.getFileNameFromHttpResponse(result);
    document.body.appendChild(a);
    a.click();
    $scope.isReportInProgress = false;
};

The file is working on most of the cases but for some reason when the file is larger than 10MB i get "Failed - Network Error".

It happens only on chrome.

I tried to search the web for this issue and couldn't find anything relevant.

Can you think of an idea why does it happens? or maybe use a different save file method that will work on chrome/firefox/IE instead of my function?

like image 677
Liad Livnat Avatar asked Sep 07 '16 15:09

Liad Livnat


1 Answers

I was finally used this one, hope it can help the next one encounter this issue:

 var blob = new Blob([result.data], {type: 'text/csv'});
            var filename =  $scope.getFileNameFromHttpResponse(result);
            if(window.navigator.msSaveOrOpenBlob) {
                window.navigator.msSaveBlob(blob, filename);
            }
            else{
                var elem = window.document.createElement('a');
                elem.href = window.URL.createObjectURL(blob);
                elem.download = filename;
                document.body.appendChild(elem);
                elem.click();
                document.body.removeChild(elem);
            }
like image 131
Liad Livnat Avatar answered Oct 19 '22 23:10

Liad Livnat