function downloadFIle(url) {
if (!url) {
return console.error(
'The parameters passed in are not correct, the function does not work properly.'
);
}
let a = document.createElement('a');
a.href = url;
a.target = '_black';
if (document.all) {
a.click();
} else {
// 兼容 Firfox
var evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, true);
a.dispatchEvent(evt);
}
}
a标签的
download
是HTML5
较新的属性,有一定的兼容性和同源策略跨域的问题。如果后端有在
header
表示图片资源的话,可以使用下面这个函数:关于这个属性,张鑫旭大佬也有一篇文章讲过,可以了解一下