下载文件流代码实现
function downFile(res, fileName) { const blob = new Blob([res], { type: `application/pdf;charset=UTF-8` }); if (window.navigator.msSaveOrOpenBlob) { // 兼容ie11 window.navigator.msSaveOrOpenBlob(blob, fileName); } else { const url = URL.createObjectURL(blob); const downloadElement = document.createElement("a"); downloadElement.href = url; downloadElement.download = fileName; document.body.appendChild(downloadElement); downloadElement.click(); downloadElement.remove(); URL.revokeObjectURL(url); } } try { this.$http.post("/api/company/bill/download", { id, }, { responseType: 'blob' }).then((res) => { this.showLoading = false; downFile(res, `123.pdf`) return; }); } catch (error) { console.error('error: ', error); }
重点在于 responseType : 'blob' ,指定已 blob 形式接收