i have a csv file link "http://spatialkeydocs.s3.amazonaws.com/FL_insurance_sample.csv.zip". my application is angularjs appliction, for downloading this i have used "download" attribute it is working fine in chrome and firefox. but this is not working in the IE and safari because download attribute is not supported by the IE and Safari browsers. could you guys please suggest me how to make it work for IE and safari. if possible can one share plunker ? Thanks in advance !
Asked
Active
Viewed 3,035 times
-1
-
Please! go through stackoverflow link - http://stackoverflow.com/questions/18394871/download-attribute-on-a-tag-not-working-in-ie – Utkarsh Dubey Mar 30 '17 at 05:30
-
Please! delete this question and make some research before asking question in stack overflow. – Utkarsh Dubey Mar 30 '17 at 05:32
-
@UtkarshDubey Do not delete this question. It's marked as a duplicate, and we move on. Do not deter the user from asking questions even if accidentally it is a duplicate. – Con Antonakos Jun 19 '18 at 20:37
1 Answers
3
Here is the code I used on my last project. It's not perfect, but it passed QA in all browsers and IE9+.
downloadCSV(data,fileName){
var blob = new Blob([data], {type: "text/plain;charset=utf-8;"});
var anchor = angular.element('<a/>');
if (window.navigator.msSaveBlob) { // IE
window.navigator.msSaveOrOpenBlob(blob, fileName)
} else if (navigator.userAgent.search("Firefox") !== -1) { // Firefox
anchor.css({display: 'none'});
angular.element(document.body).append(anchor);
anchor.attr({
href: 'data:attachment/csv;charset=utf-8,' + encodeURIComponent(data),
target: '_blank',
download: fileName
})[0].click();
anchor.remove();
} else { // Chrome
anchor.attr({
href: URL.createObjectURL(blob),
target: '_blank',
download: fileName
})[0].click();
}
}
Using the ms specific API worked best for us in IE. Also note that some browsers require the anchor to actually be in the DOM for the download attribute to work, whereas Chrome, for example, does not. Also, we found some inconsistencies with how Blobs work in various browsers. Some browsers also have an export limit. This allows the largest possible CSV export in each browser afaik.

Kevin
- 1,195
- 12
- 14