getdownload() {
let downloadParams = {
productCode: productcode,
reportId: reportId,
startTime: startTime,
endTime: endTime
};
reportApi.reportResultExport(downloadParams).then(res=>{
let blob =new Blob([res.data],{type:"application/vnd.ms-excel;charset=utf-8"});
if(window.navigator.msSaveBlob){
window,navigator.msSaveBlob(blob,this.reportName+".xls");
}else{
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.style.display = 'none';
link.href = url;
link.download = this.reportName+".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
console.log(typeof (res.data));
}).catch(err=>{
console.log(err);
})
},
请求接口
/**
* 下载
* @param data
*/
export function reportResultExport(data) {
return request({
url: '/api/v1/rpt/reportResultExport',
method: 'post',
data,
code: "v4-000",
responseType: 'blob'
})
}
注意
service.interceptors.response.use(
response => {
if(response.config.responseType=='blob'){
if(response.status==200){
return response;
}else{
return Promise.reject(error);
}
}else{
const res = response.data;
if (res.code === 200) {
return response.data
} else if(res.code === 407){
// console.log("Token失效跳转登陆页面");
// sessionStorage.clear();
// location.href="/ecloud-sp/logout";
} else {
Message({
message: res.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject('error')
}
}
},
error => {
console.log('err' + error) // for debug
// if (axios.isCancel(error)) {
// // 为了终结promise链 就是实际请求 不会走到.catch(rej=>{});这样就不会触发错误提示之类了。
// return new Promise(() => {});
// } else {
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
// }
}
)
在做下载excel的时候,一定要在response 拦截器判断responseType,有可能后台返回的数据流就是res