1、设置接口response返回类型 responseType: 'blob'
fetchResourceDownLoad(opt) {
return Vue.http.post(`${baseUrl}/userResource/download`, opt, { responseType: 'blob' });
},
2、请求接口,创建a链接进行下载
download(params) {
const options = {
resourceId: params.resourceId,
};
fetchResourceDownLoad(options).then((res) => {
const a = document.createElement('a');
const fileName = params.fileName;
if ('download' in a) {
a.download = fileName;
a.style.display = 'none';
a.href = window.URL.createObjectURL(res.body);
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}
});
},