通常情况blob后台文件流都是如下图这样让人崩溃的乱码
导出
// 导出----------------------------------------
handleExport() {this.loading = true;const data = {// 导出----------------------------------------/*file_name:"推广记录(" +new Date().toLocaleString("zh-Hans-CN", {year: "numeric",month: "2-digit",day: "2-digit",hour12: false,hour: "2-digit",minute: "2-digit",second: "2-digit",}).replace(///g, "-") +").xlsx",*/// ----------------------------------------gid: this.searchForm.gid,gname: this.searchForm.gname,type: this.searchForm.type,status: this.searchForm.status,num: this.searchForm.num,hy: this.searchForm.hy,remark: this.searchForm.remark,};this.$axios.post("https://xxx.com/getlist?export_flag=1",data,{ responseType: "blob" }).then((d) => {if (!d.data) return (this.loading = false,this.$message.error(d.msg)); //导出失败报错!// 自动下载blob文件流xls文件----------------------------------------const blob = new Blob([d.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",});//防止下载的文件乱码const url = window.URL.createObjectURL(blob);const link = document.createElement("a");link.style.display = "none";link.href = url;link.setAttribute("download",`导出文件(${new Date().toLocaleString("zh-Hans-CN", {year: "numeric",month: "2-digit",day: "2-digit",hour12: false,hour: "2-digit",minute: "2-digit",second: "2-digit",}).replace(///g, "-").replace(/:/g, "")}).xlsx`//后缀名一定要用xlsx,否则会乱码); //设置下载文件名document.body.appendChild(link);link.click();document.body.removeChild(link); // 下载完成移除元素window.URL.revokeObjectURL(url); // 释放掉blob对象//----------------------------------------this.$message.success("导出xlsx成功");this.loading = false;});
},
// ----------------------------------------
// 下载blob文件流(暂不支持手机H5唤起下载文件!!!) downloadFile(res: any, fileName: any = '未命名', format: any = '.xlsx') {const blob = new Blob([res]);fileName += format;// for IEif (windo...