简体   繁体   English

数据:附件/ csv下载在Edge中不起作用

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

I want to trigger a download once I receive certain data from the API. 我希望在从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. 这种方法适用于Chrome和Firefox。 However in Edge, for some reason, it doesn't. 但是在Edge中,出于某种原因,它没有。

Why doesn't it work in Edge and how can I fix it? 为什么它不能在Edge中运行,我该如何解决?

I had the same issue, and found this great post by Danny Pule , so your code should look something like this: 我有同样的问题,发现Danny Pule的 这篇精彩帖子 ,所以你的代码应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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