主要是使用了js的new Blob方法
//如果需要适应IE的话加上这个方法
MyBrowserIsIE() {
let isIE = false;
if(navigator.userAgent.indexOf(‘compatible‘) > -1 && navigator.userAgent.indexOf(‘MSIE‘) > -1) {
isIE = true
} if (navigator.userAgent.indexOf(‘Trident‘) > -1) {
isIE = true;
}
return isIE
}
//excel文件下载
toExcell(index, row) {
axios.get(‘/***‘, { params: { ids:1,
datatype:‘DB2‘ } }) .then(function (data) { let blob = new Blob([‘\ufeff‘ + data],{
type:‘application/text‘
});
let dateStr = new Date().toLocaleDateString().replace(/\//g,‘-‘);
if (this.MyBrowserIsIE()) {
navigator.msSaveBlob(blob,‘functions_‘ + dataStr + ‘.sql‘);
} else {
let url = URL.createObjectURL(blob);
let a = document.createElement(‘a‘);
a.style.display = ‘none‘;
a.href = url;
a.setAttribute(‘download‘, ‘functions_‘ + dataStr + ‘.sql‘);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}
this.$message({
type:‘success‘,
message:‘导出成功!‘,
duration:3000
})
}) .catch(function (error) {
this.$message({
type:‘error‘,
message:‘导出失败,请检查网络!‘,
duration:3000
})
});
vue中前端实现下载excel文件,.csv文件, .sql文件
原文:https://www.cnblogs.com/songxueqing/p/13392249.html