Old question, but I thought I'd add our solution. Here is the code I used in my last project. This is not ideal, 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 specified API is best for us in IE. Also note that some browsers require the anchor to actually be in the DOM in order to use the download attribute, while Chrome, for example, does not. In addition, we found some inconsistencies in how Blobs work in different browsers. Some browsers also have export restrictions. This allows the maximum possible CSV export in every afaik browser.
Kevin Mar 30 '17 at 5:47 2017-03-30 05:47
source share