vue模块中引入
在用的地方直接调用
写在公用js里
// 下载文件
function downloadFile (obj, name, suffix) {
let m = (new Date().getMonth() + 1) < 10 ? '0'+(new Date().getMonth() + 1) : (new Date().getMonth() + 1);
let d = new Date().getDate() < 10 ? '0'+new Date().getDate() : new Date().getDate();
var date = new Date().getFullYear() + '' + m + '' + d;
const fileName = date + name + '.' + suffix;
const blob = new Blob([obj]);
if (window.navigator.msSaveOrOpenBlob) {
try {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} catch (error) {
// this.$message.error('导出失败')
}
} else {
const link = document.createElement('a');
link.style.display = 'none';
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);// 释放URL对象
document.body.removeChild(link);
}
}