//下载excel文件
downLoadFile(url: any, data: any, fileName: any) {
let other=this;//other.messageService.error("ssskkk")
return new Promise((resolve, reject) =>{
let that= this,
options: any={},
loginInfo=UserInfo.loginInfo;
let tenatInfo= AppCommon.currentTenant || "";this.httpClient.post(url, data, {
headers:new HttpHeaders().set('current_tenant', tenatInfo.tenantId)
.set('current_app', AppCommon.currentAppcode || null),
responseType:'blob',
observe:'response'}).subscribe(res=>{//console.log(res);
//var resHeaders = res.headers.toJSON() || {};
console.log(resHeaders);
/**
* 关于Blob 如何获取内容
* https://developer.mozilla.org/zh-CN/docs/Web/API/Blob
* reader.result 包含转化为类型数组的blob*/
var reader = newFileReader();
reader.readAsText(res.body,'utf-8');//reader.readAsArrayBuffer(res.body);
reader.addEventListener("loadend", () =>{
let toJson;try{/**
* 尝试将接口返回的数据转换成json格式
* 接口正常情况下返回的数据流的形式,转成json数据肯定会报错。
* catch捕获后执行文件下载。
*
* 假如能够顺利转成json,说明接口是报错状态,返回的是错误数据*/toJson=JSON.parse(reader.result)
}catch(error) {/** 获取reseponse headers中的 文件名*/
//console.log("ssss");
//alert("ssssssssssss");
let disposition = res.headers.get('content-disposition');
let status_code=res.headers.get('status_code');//console.log(status_code);
//let resHeaders = res.headers.toJSON() || {};
if(status_code=="11060"){//if(status_code==null){
//console.log("ksjk");
//that.messageService.error("ssssssssssss");
//报错
let status_msgs = res.headers.get('status_msg') ||res.headers.get('Status_msg')||[];
let status_msg= status_msgs[0] || "";
let base64= newBase64();
status_msg= base64.decode(status_msg) || "服务异常";
resolve({ error_msg:"数据量过大,不能正常导出,请缩小查询范围"})return;
}
let newfileName= '';if(disposition) {
newfileName= decodeURI(disposition.split('=')[1]);//newfileName=newfileName.substring(0,fileName.length);
fileName =newfileName;
}this.downFile(res.body, fileName);//resolve();
return null;
}//报错
reject(toJson.msg);
});
}, err=>{
reject(err.msg);
})
});
}