<!DOCTYPE html>
<head>
<title>测试下载</title>
</head>
<body>
<button id="downloadId" onclick="downloadABMFile()">下载</button>
<script type="text/javascript">
function downloadABMFile(){
alert("下载开始")
const xhr = new XMLHttpRequest()
xhr.open('POST', 'https:///download', true)
xhr.responseType = 'blob'
xhr.onload = function () {
if (this.status === 200) {
const blob = this.response
const a = document.createElement('a')
const url = window.URL.createObjectURL(blob)
a.href = url
a.download = 'test.xlsx'
a.click()
window.URL.revokeObjectURL(url)
}
}
xhr.onerror = function () {
alert("下载出错了");
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (this.status === 500) {
alert("服务器错误")
}
}
};
const formData = new FormData();
formData.append('id', '1203f65065e54ee6964f7ad027c4e7bf');
xhr.send(formData)
}
</script>
</body>
</html>