如果不需要权限,直接通过链接下载
window.location.href='url?id=123...';
//或者
this.$router.push({
path:'/url',
query:this.data
});
需要权限,则通过ajax请求,拿到流数据
获取数据的时候,一定要加上(responseType: 'blob'),表示后台传过来的数据用 blob 对象接收.
axios.post(`接口路径`, {
responseType: 'blob'
});
写个公共的方法
downloadExcel(fileName, data) {
let blob = new Blob([data], { type: "application/vnd.ms-excel" });
let fileUrl = URL.createObjectURL(blob);
let a = document.createElement("a");
a.setAttribute("href", fileUrl);
a.setAttribute("download", fileName);
a.click();
}
调用的时候,直接传入名字和数据
Csv.downloadExcel("列表", data.data);