//下载excel
async getDownload(ctx, formData, t) {
let params = {
path:'INeonatalReportRecordService/download',
data: formData,
}
// let result = await ctx.Api.api.post(params);
let xhr = new XMLHttpRequest();
let fileIds = '6170fd10a856a800010ff5f2';
let token = getToken();
let action = `${FILE_URL}/${fileIds}?token=${ token }`; //下载文件的接口地址
xhr.open("GET", action);
xhr.responseType = "blob";
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && xhr.status==200){
let resultObj = xhr.response;
console.log(resultObj)
t.downloadExcel(resultObj)
//处理返回的数据......
}
}
xhr.send();
},
downloadExcel= (data) => {
if (!data) {
return
}
var blob = new Blob([data], { type: 'application/vnd.ms-excel' });
var url = window.URL.createObjectURL(blob);
var aLink = document.createElement("a");
aLink.style.display = "none";
aLink.href = url;
aLink.setAttribute("download", document.title + ".xlsx");
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink); //下载完成移除元素
window.URL.revokeObjectURL(url); //释放掉blob对象
}
JS下载文件到本地
最新推荐文章于 2024-06-18 15:07:52 发布