前端接收下载blob文件流

 API文件内

//敏感词 导出
import axios from "axios";

axios.defaults.baseURL = "http://cms.dtcg.7os.com.cn/cms/admin";


export let downFile = () =>
  axios({
    method: "get",   //请求方式
    url: "/word/export",   //请求接口
    responseType: "blob",  //不可省略
  });

 

在需要下载的页面 导入下载文件接口 (思路  通过a标签的链接向后端服务发get请求  模拟点击触发 完后下载 下载完成之后 移出a标签节点)

import {downFile} from "../../api/index.js";
  
// 点击导出
    async downfile() {
      let response = await downFile();
      let res = response.data;

      //获取请求的类型
      const respContentType = response.headers["content-type"];

      if (
        respContentType ===
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
      ) {
        if (!res) {
          return;
        }
        //由于后台返回文件名称是通过response返回的
        //因此需要从response headers中content-disposition响应头中获取文件名称fileName 而我的后端是写死的文件名 并且没有放进响应请求头返回给前端 所以前端我也写死了
        let fileName = "敏感词";

        const url = window.URL.createObjectURL(
          new Blob([res], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
          })
        );
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        //decodeURIComponent解决文件名的url转码问题
        link.setAttribute("download", decodeURIComponent(fileName));
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        return;
      }
    },
  },

二次封装 element的提示  因为我的是后端 返回success为true或false来判断成功失败

   //操作提示
    Tip(tipword, type) {
      switch (type) {
        case true:
          this.$message({
            message: tipword,
            type: "success",
          });
          this.reload();
          break;
        case false:
          this.$message({
            message: tipword,
            type: "error",
          });
          this.reload();
          break;
      }
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值