通过判断浏览器类型,如果为ie,采用另存为的方式达到下载效果。其他浏览器则模拟点击下载。
function downloadFile(title, data) {
const blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;'});
const ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),
ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
ieEDGE = navigator.userAgent.match(/Edge/g),
ieVer = (ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));
console.log('ie:' + ie);
console.log('ieVer:' + ieVer);
if (ie && ieVer < 10) {
this.message.error('No blobs on IE<10');
return;
}
if (ieVer > -1) {
window.navigator.msSaveBlob(blob, title);
} else {
const url = window.URL.createObjectURL(blob);
let link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', title);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
说明:title为文件名称;data为回调response,通过this.response获取。