vue根据后端返回的base64图片数据转成图片并下载

下载后端返回的base64格式的图片

    downloadLogo(id) {
      let json = {
        id: id,
      };
      this.download(json).then((res) => {
        console.log(res, "======sss=======");
        // 这里是获取到的图片base64编码res.file,这里只是个例子哈,要自行编码图片替换这里才能测试看到效果
        const imgUrl = `data:image/jpg;base64,${res.file}`;
        // 如果浏览器支持msSaveOrOpenBlob方法(也就是使用IE浏览器的时候),那么调用该方法去下载图片
        if (window.navigator.msSaveOrOpenBlob) {
          const bstr = atob(imgUrl.split(",")[1]);
          let n = bstr.length;
          const u8arr = new Uint8Array(n);
          while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
          }
          const blob = new Blob([u8arr]);
          window.navigator.msSaveOrOpenBlob(blob, "logo图片.jpg");
        } else {
          // 这里就按照chrome等新版浏览器来处理
          const a = document.createElement("a");
          a.href = imgUrl;
          a.setAttribute("download", "logo图片.jpg");
          a.click();
        }
      });
    },

原文参考:https://blog.csdn.net/xiaozhidian/article/details/129166541?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-5-129166541-blog-95431330.235%5Ev38%5Epc_relevant_anti_vip_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-5-129166541-blog-95431330.235%5Ev38%5Epc_relevant_anti_vip_base&utm_relevant_index=10

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值