ajax请求获取文件流并做下载处理
test() {
axios({
method: 'post',
url: url,
data: {},
responseType: 'blob'
}).then(res => {
let data = res.data;
let url = window.URL.createObjectURL(newBlob([data]));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', 'test.rar');
document.body.appendChild(link);
link.click();
}).catch((error) => {})
}
指定返回数据的格式为blob
promise = axios.get(url,{
responseType: 'blob' //指定返回数据的格式为blob
});