下载excel表格
先从后端获取文件流,获取数据为blob格式,然后创建a窗口,命名要下载的文件,最后移除a标签
代码如下:
javascript
downLoad() {
let params = {
model_name: "a"
}
this.$axios
.get(`url`, { responseType: "blob", params})
.then((res) => {
console.log(res.data)
const link = document.createElement("a");
let blob = new Blob([res.data], {
type: "application/xlsx,charset=utf-8",
});
link.style.display = "none";
link.href = URL.createObjectURL(blob);
// 默认下载时的名称及后缀
link.download = 'xxx' + ".xlsx";
document.body.appendChild(link);
//模拟点击事件
link.click();
//移除创建的a标签
window.URL.revokeObjectURL(link.href);
document.body.removeChild(link);
});
}