下载pdf文档

<el-button
  link
  type="primary"
  size="large"
  @click="downProfile(row.resId)"
   >下载</el-button>
const downProfile = async (id) => {
  try {
    // const res = await proxy.$http.playerManage.downloadCompetitionProfile(id);
    // 直接使用 axios 发送请求,并设置 responseType 为 blob
    const apiBaseUrl = import.meta.env.VITE_BASE_URL_API;
    const token = userStoreData.user.token;
    const res = await axios.get(`${apiBaseUrl}/mark-resource/down/${id}`, {
      responseType: "blob", // 设置响应类型为 blob(文件流)
      headers: {
        Authorization: ` ${token}`, // 添加 Authorization 头
      },
    });
    if (res) {
      // 获取响应头中的文件名
      const contentDisposition = res.headers["content-disposition"];
      let fileName = "资料文件.pdf"; // 默认文件名
      if (
        contentDisposition &&
        contentDisposition.indexOf("attachment") !== -1
      ) {
        const match = contentDisposition.match(/filename="(.+)"/);
        if (match && match[1]) {
          fileName = decodeURIComponent(match[1]); // 解码文件名
        }
      }

      //创建一个Blob对象,并设置MIME类型为PDF
      const blob = new Blob([res.data], { type: "application/pdf" });
      //创建一个URL对象,用于指向Blob
      const url = URL.createObjectURL(blob);

      //创建一个影藏的a标签触发点击事件下载文件
      const link = document.createElement("a");
      link.href = url;
      link.download = fileName; //设置下载文件名
      link.click();

      //释放URL对象
      URL.revokeObjectURL(url);
    }
  } catch (error) {
    showMessage({
      type: "error",
      message: "导出失败,请重试",
    });
  }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值