import axios from 'axios'
import { getToken } from '@/utils/auth'
// 下面是封装
export function downFile(data) {
axios.defaults.headers.common['token'] = getToken()
return axios({
method: 'post',
url: process.env.VUE_APP_BASE_API + '路径',
responseType: 'blob',
timeout: 30000,
data: data
})
}
// 下面是调用
downFile(data).then(res => {
const content = res.data
const blob = new Blob([content], { type: 'application/x-xls' })
const fileName = 'excel文件.xlsx'
if ('download' in document.createElement('a')) {
const link = document.createElement('a')
link.download = fileName
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
document.body.removeChild(link)
} else {
navigator.msSaveBlob(blob, fileName)
}
})