2021/05/31 通过PDF外链实现PDF文件下载

  • 背景
    –1.请求接口得到的数据是一个指向PDF文件,存放在阿里云oss 上的链接。类似如下
    url: “http://aaa.bbb.com/dedfa95299474609a7ada88ab4ed7c921620895919739.pdf”
    在这里插入图片描述

–2.get请求该链接,会得到返回的文件流。文件流类似这种
在这里插入图片描述


  • 我是直接用fetch请求的
    代码
//获取blob文件流 下载PDF文件 成功
    fetchBlob() {
      //不传cookie
      window
        .fetch(this.pdfUrl, {
          method: "get",
          responseType: "blob",
        })
        .then((res) => {
          console.log("blob res", res);
          return res.blob();
        })
        .then((blob) => {
          console.log("下载 blob", blob);
          let bl = new Blob([blob], { type: "application/pdf" });
          //自定义修改 命名规则
          let fileName = Date.parse(new Date()) + ".pdf";
          var link = document.createElement("a");
          link.href = window.URL.createObjectURL(blob);
          link.download = fileName; //时间戳命名
          link.click();
          window.URL.revokeObjectURL(link.href);
        });
    },

–如果遇到报错
报错信息:not be the wildcard ‘*’ when the request’s credentials mode is ‘include’.
在这里插入图片描述
我这里是由于封装的组件,默认请求在请求头中携带了cookie
在这里插入图片描述
所以也能直接用window.fetch来获取文件流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值