import axios from 'axios' import qs from 'qs' import * as $$ from '@/utils/utility'; export default function downloadFile(url, params) { const link = document.createElement('a') return axios({ url, params, method: 'GET', headers: { Authorization: $$.getSessionStorage('customerSystemToken') }, responseType: 'blob', paramsSerializer: (v) => qs.stringify(v, { arrayFormat: 'repeat' }), }) .then((res) => { // 切割文件名 const fileNameEncode = res.headers['content-disposition'].split('filename=')[1] // 解码 const fileName = decodeURIComponent(fileNameEncode) // 设置type类型 const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; application/octet-stream', }) const fileUrl = window.URL.createObjectURL(blob) link.href = fileUrl link.setAttribute('download', fileName) link.style.display = 'none' link.click() link.remove() }) .catch(() => { $$.info({ type: 'error', content: '文件下载请求失败' }); }) }