data:attachment/csv download doesn't work in Edge

Viewed 1735

I want to trigger a download once I receive certain data from the API. I do it the following way:

const anchor = window.angular.element('<a/>');
anchor.css({display: 'none'});
window.angular.element(document.body).append(anchor);

anchor.attr({
  href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
  target: '_blank',
  download: 'somedata.csv',
})[0].click();

anchor.remove();

This approach works both in Chrome and Firefox. However in Edge, for some reason, it doesn't.

Why doesn't it work in Edge and how can I fix it?

1 Answers

I had the same issue, and found this great post by Danny Pule, so your code should look something like this:

if (navigator.msSaveBlob) { // IE 10+
    var exportedFilenmae = 'somedata.csv';
    var blob = new Blob([data], { type: 'text/csv;charset=utf-8;' });
    navigator.msSaveBlob(blob, exportedFilenmae);
} else {
    ..........
}
Related