// 文件下载,url为文件地址,可以为本地地址也可以为网络地址
function downloadFile(url) {
// 是否ie:fetch不支持ie
if (isIE()) {
window.open(url, '_blank')
return
}
// 文件名
// let filename = `xxx.txt`;
return fetch(url).then((res) =>
res.blob().then((blob) => {
let a = document.createElement("a");
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
})
);
}
function isIE () { // ie?
if (!!window.ActiveXObject||'ActiveXObject'inwindow) { returntrue } else { returnfalse }
}