JS使用a标签下载文件
Dowload(url, fileName) { let xmlResquest = new XMLHttpRequest(); xmlResquest.open('GET', url, true); xmlResquest.setRequestHeader('Content-type', 'application/json'); xmlResquest.setRequestHeader( 'Authorization', this.$store.getters.getToken() ); xmlResquest.responseType = 'blob'; xmlResquest.onload = function (oEvent) { if (xmlResquest.status != 200) { this.$message({ type: 'error', message: '下载文件出错了..' }); return; } const downloadLink = document.createElement('a'); let content = xmlResquest.response; let blob = new Blob([content]); downloadLink.href = URL.createObjectURL(blob);; downloadLink.setAttribute('download', fileName); document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); }; xmlResquest.send(); }