封装的方法
download(res, type, filename) {// 创建blob对象,解析流数据const blob = new Blob([res.data], {// 如果后端没返回下载文件类型,则需要手动设置:type: 'application/pdf;chartset=UTF-8' 表示下载文档为pdf,如果是word则设置为'application/msword',zip为 'application/zip'type: type})console.log(blob,'blob');const a = document.createElement('a')// 兼容webkix浏览器,处理webkit浏览器中href自动添加blob前缀,默认在浏览器打开而不是下载const URL = window.URL || window.webkitURL// 根据解析后的blob对象创建URL 对象const herf = URL.createObjectURL(blob)console.log(herf,'herf');// 下载链接a.href = herfa.setAttribute("download", filename);document.body.appendChild(a)a.click()document.body.removeChild(a)// 在内存中移除URL 对象window.URL.revokeObjectURL(herf)},
1,一开始没有加responseType: “blob”
exportPdf() {this.loading = true; //loading 防止重复点击http({method: "get",url: `/contract/export/pdf?id=${this.detailInfo.id}`,responseType: "blob",}).then(async (res) => {console.log("window.open", res);await this.common.download(res,"application/pdf;chartset=UTF-8",`${this.detailInfo.contractNo}.pdf`);this.loading = false;});// .catch((e) => {// this.message.error("请求超时");// });},
但是如下还是空白
没有找出是为什么,用了如下方法
window.location.href = `${glo.weburl}/contract/export/pdf?id=${this.detailInfo.id}`;
参考文档:https://blog.csdn.net/m0_62317155/article/details/128885376