XMLHttpRequest下载文件

//表单数据
var formData = new FormData();

formData.append("whereStr", "aaa");
formData.append("orderStr", "bbb");


//打开加载loading框 parent.$.messager.progress({ title: '提示', text: '正在导出数据中,请稍后....' }); var xmlRequest = new XMLHttpRequest(); xmlRequest.open('post', _url_export + _batchId, true);
xmlRequest.send(formData); xmlRequest.responseType
= 'blob'; xmlRequest.onreadystatechange = function () { if (xmlRequest.readyState == 4 && xmlRequest.status == 200) { // 数据在 this.response 保存 // excel 的 MIME 格式为 application/vnd.ms-excel var blob = new Blob([this.response], { type: "application/vnd.ms-excel" }); // 创建a链接 href链接地址 download为下载下来后文件的名称 var aa = document.createElement('a'); aa.href = URL.createObjectURL(blob); aa.innerHTML = 'a链接'; aa.download = '导出数据.xlsx'; aa.style.display = 'none'; //隐藏a标签 直接调用a标签的点击事件 document.body.appendChild(aa); aa.click(); } else { //文件流结束关闭loading加载框 parent.$.messager.progress('close'); } }

 

posted @ 2023-03-14 17:44  教练我想打篮球  阅读(711)  评论(0编辑  收藏  举报