后端返回文件流,前端导出excel

  1. 请求
  1. 检查接口
  1. 导出代码

(如果可以导出,且表格为undefined,把new Blob([res]换成new Blob([res.data], 或者在检查后端是不是返回的blob对象)

复制代码
ExportWaterEventListPage(data).then(res => {
        // console.log("导出闷盖记录为excel", res);
        // 转化为blob对象
        let blob = new Blob([res], {
          type: "application/octet-stream"
        });
        let filename = "放水分析记录.xls";
        // 将blob对象转为一个URL
        var blobURL = window.URL.createObjectURL(blob);
        // 创建一个a标签
        var tempLink = document.createElement("a");
        // 隐藏a标签
        tempLink.style.display = "none";
        // 设置a标签的href属性为blob对象转化的URL
        tempLink.href = blobURL;
        // 给a标签添加下载属性
        tempLink.setAttribute("download", filename);
        if (typeof tempLink.download === "undefined") {
          tempLink.setAttribute("target", "_blank");
        }
        // 将a标签添加到body当中
        document.body.appendChild(tempLink);
        // 启动下载
        tempLink.click();
        // 下载完毕删除a标签
        document.body.removeChild(tempLink);
        window.URL.revokeObjectURL(blobURL);
        this.$message({
          message: "导出成功!",
          type: "success"
        });
      });
相关推荐
Mintopia1 分钟前
Next.js 全栈:接收和处理请求
前端·javascript·next.js
袁煦丞35 分钟前
2025.8.18实验室【代码跑酷指南】Jupyter Notebook程序员的魔法本:cpolar内网穿透实验室第622个成功挑战
前端·程序员·远程工作
Joker Zxc40 分钟前
【前端基础】flex布局中使用`justify-content`后,最后一行的布局问题
前端·css
无奈何杨43 分钟前
风控系统事件分析中心,关联关系、排行、时间分布
前端·后端
Moment1 小时前
nginx 如何配置防止慢速攻击 🤔🤔🤔
前端·后端·nginx
晓得迷路了1 小时前
栗子前端技术周刊第 94 期 - React Native 0.81、jQuery 4.0.0 RC1、Bun v1.2.20...
前端·javascript·react.js
前端小巷子1 小时前
Vue 自定义指令
前端·vue.js·面试
玲小珑1 小时前
Next.js 教程系列(二十七)React Server Components (RSC) 与未来趋势
前端·next.js
Mike_jia1 小时前
UptimeRobot API状态监控:零成本打造企业级业务健康看板
前端