简体   繁体   中英

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

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?

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 {
    ..........
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM