function downLoadFile(url,params){ var xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.responseType = "blob"; // 返回类型blob xhr.onload = function () { if (xhr.status === 200) { const dispoition = xhr.getResponseHeader('Content-Disposition') || '' const nameStr = dispoition.split(';')[1] || '' const fileName = decodeURI(nameStr.split('=')[1] || '') console.log(fileName,'乱码') //decodeURI解码需要后端处理。文件类型固定可自定义名字 const blob = new Blob([xhr.response], { type: "text/html; charset=UTF-8" }) const a = document.createElement('a') a.href = URL.createObjectURL(blob) a.download = fileName //a.download = '历史数据.xlsx'; // 成功 a.click(); } else { //请求失败处理 } }; // 发送ajax请求 xhr.send(JSON.stringify(params)) } |
|