export async function downloadTwo(url, params, filename) {
const res = await axios({
headers: {
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'token'
},
method: 'post',
url: url, // 请求路径
data: params,
responseType: 'blob',
});
const fileNameEncode = res.headers['content-disposition']
.split('filename=')[1]
.split(';')[0];
const fileName = decodeURI(fileNameEncode, 'utf-8').replaceAll('"', '')
const blob = new Blob([res.data]);
const urls = URL.createObjectURL(blob);
const link = document.createElement('a');
link.style.display = 'none';
link.setAttribute('href', urls);
link.setAttribute('download', fileName);
link.click();
URL.revokeObjectURL(urls);
}
vue在axios请求下载文件时,通过headers的[content-disposition]获取文件名称
于 2024-11-21 15:26:04 首次发布