关于angular2内下载功能兼容ie11 和 火狐点击事件:
const uA = window.navigator.userAgent//判断浏览器内核
const isIE = /msie\s|trident\/|edge\//i.test(uA) && !!("uniqueID" in document || "documentMode" in document || ("ActiveXObject" in window) || "MSInputMethodContext" in window);
var a = document.createElement('a');
var blob = new Blob([data], {'type':"application/vnd.ms-word"});
// window.open(URL.createObjectURL(blob),'hide')
a.href = URL.createObjectURL(blob);
a.download = title2+".doc";
//此处是为了兼容火狐点击事件;
document.body.appendChild(a);
if (isIE) {
// 兼容IE11无法触发下载的问题
navigator.msSaveBlob(new Blob([data]),a.download);
} else {
a.click();
alert('下载完成');
}
// 触发下载后再释放链接
a.addEventListener('click', function() {
URL.revokeObjectURL(a.href);
document.getElementById('download').remove();
});
本文介绍了一种在Angular2中实现文件下载功能的方法,该方法能够兼容IE11和火狐浏览器。通过检测浏览器类型,使用不同的技术来触发文件下载,包括利用Blob对象创建下载链接和针对IE11的navigator.msSaveBlob方法。
1311

被折叠的 条评论
为什么被折叠?



