获取后端返回文件流下载文件至本地
responseType: ‘blob’ 注意设置类型为blob
// responseType: 'blob'注意请求类型为blob
exportCheckBill(obj)
.then(response => {
// 处理请求返回的文件流
const content = response;
const blob = new Blob([content]);
const fileName = `文件名.xlsx`;
if ("download" in document.createElement("a")) {
const elink = document.createElement("a");
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
} else {
navigator.msSaveBlob(blob, fileName);
}
})
.then(() => {
setTimeout(() => {
loading.close();
}, 1000);
})
.catch(function() {
loading.close();
// this.msgError('下载失败')
});
通过a标签访问指定路径直接从服务器或前端项目静态文件下载文件
<a :href="`${本地路径}data/文件名.文件类型`" target="_blank" download="文件名.文件类型" ><el-button type="primary" icon="el-icon-download" size="mini">下载文件</el-button></a>
如果是vue-cli3的项目需要将文件放到public文件夹下的data文件夹里面(可自己定义文件名)
若是vue-cli2的则放在static文件夹下即可
若是存放在服务器上的文件, window.location = 文件在服务器的相对路径