前端文件导出(2)
前后端操作
1.后端提供导出接口
接口demo
接口地址: /api-linzhang/oneAndOne/forestRanger/export
请求方式: GET
请求参数:省略
2.前端操作
/**
* 表格导出
*/
exportExcel:function(){
// 参数 不重要
let params = {
district:that.areaCode,
nameOrMobile: that.nameOrMobile
}
this.$axios.get("接口地址", {params:params,responseType:'blob'}).then((res) => {
console.log(res); // Blob {size: 12641, type: "application/octet-stream"}
let url = window.URL.createObjectURL(new Blob([res],{type: "application/vnd.ms-excel"}))
console.log(url); // blob:http://localhost:8081/1f2736ac-da9f-4121-9524-5114f0086637
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', '护林员' + '.xlsx');
document.body.appendChild(link);
link.click();
})
},