我在第一个代码段下给出了一些JavaScript代码,这些代码在最新的Chrome浏览器中有效,但在最新的FireFox中不起作用。此代码使用Blob对象将数据导出到html文件。奇怪的是,在FireFox中,代码不会引发任何错误,但是会得到没有扩展名的文件。 Edge&IE中的相同问题
导出代码:
downloadFile(data: Response | any, fileName: string, typefile) {
const blob = new Blob([data], {type: typefile});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, fileName);
} else {
const url = window.URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.download = fileName;
anchor.href = url;
document.body.appendChild(anchor);
anchor.dispatchEvent(new MouseEvent(`click`, {bubbles: true, cancelable:
true, view: window}));
document.body.removeChild(anchor);
setTimeout(function() {window.URL.revokeObjectURL(url); }, 0);
}
}
答案 0 :(得分:1)
我使用了以下代码,并在Edge和Firefox中进行了测试,并且在Safari(Ipad上的Safari除外)中都可以正常工作
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(data, filename);
}
else {
var url = window.URL.createObjectURL(data);
var a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display: none');
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
}
答案 1 :(得分:-1)
这包括Safari> = 10,Chrome> = 55(包括Opera), * Edge> = 13(在台式机上),iOS 10和Chrome在移动版上。
进一步了解https://angular.io/guide/browser-support
/ ** IE9,IE10和IE11需要以下所有填充。 ** /
请在polyfills.ts中添加以下代码
导入'core-js / es6 / reflect'; 导入'core-js / es7 / reflect';
导入'zone.js / dist / zone';