通常情况blob后台文件流都是如下图这样让人崩溃的乱码
<el-button type="success" @click.stop="handleExport" icon="el-icon-download" >导出</el-button>
// 导出---------------------------------------- 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 a= document.createElement("a"); a.href = URL.createObjectURL(blob); a.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, "")}).xlsx`//后缀名一定要用xlsx,否则会乱码 ); //设置下载文件名 a.click(); //---------------------------------------- this.$message.success("导出xlsx成功"); this.loading = false; }); }, // ----------------------------------------