axios({
url: '/api/download',
method: 'GET',
responseType: 'blob'
}).then(response => {
const disposition = response.headers['content-disposition'];
const filename = disposition.match(/filename=(.*)/)[1];
const downloadUrl = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
})
以上代码将通过axios发送GET请求并获取完整的response对象,通过正则表达式获取content-disposition头中的文件名,并使用Blob对象和对象URL创建一个下载链接,最后触发点击该链接的事件以实现文件下载。