axios导出Excel或word
1.通用方法书写
import axios from 'axios'
import { saveAs } from 'file-saver'
// 创建axios实例
const request = axios.create({
baseURL: 'http://192.168.0.101:8081/zj_travel',// 所有的请求地址前缀部分(没有后端请求不用写)
timeout: 80000, // 请求超时时间(毫秒)
//withCredentials: true,// 异步请求携带cookie
headers: {
'Content-Type': 'application/json',
},
})
// request拦截器
request.interceptors.request.use(
...
)
// response 拦截器
request.interceptors.response.use(
...
)
//通用导出方法
/* eslint-disable */
export function download(url:string, data:object, filename:string, config:object) {
return request.post(<any>url, data, {
headers: { 'Content-Type': 'application/json' },
responseType: 'blob',
...config
}).then(async (data) => {
const blob = new Blob([<any>data])
saveAs(blob, <any>filename)
}).catch((r) => {
console.log(r)
})
}
export default request
ps:需要安装axios插件 略
需要安装file-saver插件
npm install file-saver --save
npm i --save-dev @types/file-saver
2.调用
const data={statisTime:ruleForm.yeartime+ruleForm.monthtime,areaId:ruleForm.name,}
download('/global/wordExport',data,"省级信息.docx",{})
标签:axios,word,request,saver,Excel,file,const,data
From: https://www.cnblogs.com/WangJingjun/p/18007534