vue下载后端给的excel(非前端生成excel)
程序员文章站
2024-03-20 17:44:28
...
vue下载后端给的excel
主要是请求回来文件流的处理
html:
<el-button size="mini" @click="downexcel">模板下载</el-button>
js:
downexcel() {
axios({
method: "get",
url: SERVER_BASE_URL + "invoice/saledetail/tempDownload",//请求后端地址SERVER_BASE_URL自己的端口
// headers里面设置token
headers: {
"Content-Type": "multipart/form-data",
token: getToken()//token
},
// 二进制流文件,一定要设置成blob,默认是json
responseType: "blob"
}).then(res => {
const link = document.createElement("a");
const blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
link.style.display = "none";
link.href = URL.createObjectURL(blob);
link.setAttribute("download", "xxx模板.xls");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
},