Angular save file as csv result in Failed- network error Only on Chrome
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); }
I had a similar problem, but had to serve a zip file, sent from the server in base64 format. What did the trick for me:
function downloadBlob(data, filename) { const blob = new Blob([new Buffer(data, 'base64')], {type: 'application/octet-stream'}); if (window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveBlob(blob, filename); } else { const elem = window.document.createElement('a'); elem.href = window.URL.createObjectURL(blob); elem.download = filename; document.body.appendChild(elem); elem.click(); window.URL.revokeObjectURL(elem.href); document.body.removeChild(elem); } }return ( {/* ... */} <a href="#" onClick={() => {downloadBlob(report.zip, `${report.filename}.zip`)}}> Download <b>{report.filename}.zip</b> </a> {/* ... */});
I left the function because like the fact that it revokes the URL after serving the file.