- 创建request.js公共请求文件
import axios from 'axios'
import Qs from 'qs'
axios.withCredentials = true;
axios.defaults.withCredentials = true;
axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
axios.defaults.crossDomain = true;
axios.defaults.withCredentials = true;
axios.defaults.headers.common['Authorization'] = '';
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 100000
})
export function download(url, params, filename) {
return service.post(url, params, {transformRequest: [(params) => {
return Qs.stringify(params)
}],
responseType: 'blob',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}).then((data) => {
const content = data
const blob = new Blob([content])
if ('download' in document.createElement('a')) {
const elink = document.createElement('a')
elink.download = filename
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
} else {
navigator.msSaveBlob(blob, filename)
}
}).catch((r) => {
console.error(r)
})
}
- main.js引入文件
import { download } from '@/utils/request'
- 使用
handleExport() {
this.download('/test/export', {
...this.queryParams
}, `test_link.xlsx`)
},