平时都是使用get请求来下载文件,但是要传很多参数的时候就会很复杂,使用post请求来下载文件也是可以的
// 请求还是一样返回数据流,不过在请求中设置响应类型为blob
this.$axios.post('file/downloadSomeFile', {json: params}, {responseType: 'blob'}).then(res => {
const blob = res.data;
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = (e) => {
const a = document.createElement('a');
a.download = `course_source.zip`;
a.href = e.target.result;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
})