I'm using AngularJS to create a new tag in order to download a csv file. Below the code I use to trigger the download. The download starts on Chrome but not in Firefox. Do you have any clue why this happens?
var element = angular.element('<a/>');
element.attr({
href: exportedString,
target: '_self',
download: 'test.csv'
})[0].click();
EDIT: Firefox needs an existent DOM
JS:
var linkElem = $("#link");
var element = angular.element(linkElem);
HTML:
<a ng-hide=true id="link"></a>
EDIT 2: On Chrome, the downloaded file name is "download" and not the passed value ("test.csv" in this case). Any suggestions?
Here there is also a plunker
This is a bug in Chrome 35 reported in issue #377860.
Follow this answer for more details
I updated your plunker solution.
Basically you need to use it like follow:
var element = document.createElement('a');
var blob = new Blob([$scope.exportContent], {
type: 'text/csv'
});
var url = URL.createObjectURL(blob);
element.href = url;
element.setAttribute('download', 'test.csv');
document.body.appendChild(element); //Append the element to work in firefox
element.click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With