vue3下载base64文件

如果后端明确告诉你返回的是base64,那请求头就不用带responseType: “blob”,和普通的接口一样发送就行

await materialsFile({ id: proxy.$route.query.id }).then((res) => {
    if (res) {
      // atob先解码base64数据
      const raw = window.atob(res.data);
      // 获取解码后的字符串长度
      const rawLength = raw.length;
      // 初始化一个 8 位无符号整型数组
      const uInt8Array = new Uint8Array(rawLength);
      // 将每个解码字符转换为unicode编码再存入数组中
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      const link = document.createElement("a");
      // 创建Blob对象, 固定写法
      let blob = new Blob([uInt8Array]);
      // 设置元素样式不可⻅
      link.style.display = "none";
      // 创建下载链接
      link.href = URL.createObjectURL(blob);
      // 获取⽂件名(后端应确保正确返回⽂件名)
      link.setAttribute(
        "download",
        `${userForm.value.name}-${userForm.value.phone}.zip`
      );
      // 加⼊dom树
      document.body.appendChild(link);
      // ⼿动触发点击事件
      link.click();
      // 移除之前创建的元素
      document.body.removeChild(link);
      // 释放Blob对象
      window.URL.revokeObjectURL(link.href);
    }
  });

相关推荐

  1. vue3下载base64文件

    2024-07-13 16:20:06       21 阅读
  2. Vue将File二进制文件转换为base64格式

    2024-07-13 16:20:06       54 阅读
  3. vue中,文件base64示例

    2024-07-13 16:20:06       49 阅读
  4. 微信小程序下载 base64 视频文件到本地相册

    2024-07-13 16:20:06       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 16:20:06       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 16:20:06       55 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 16:20:06       46 阅读
  4. Python语言-面向对象

    2024-07-13 16:20:06       56 阅读

热门阅读

  1. 使用 WebSocket 进行实时数据传输

    2024-07-13 16:20:06       19 阅读
  2. Redis中的管道技术

    2024-07-13 16:20:06       17 阅读
  3. C语言学习第一章--分步编译

    2024-07-13 16:20:06       16 阅读
  4. 力扣题解( 最长数对链)

    2024-07-13 16:20:06       18 阅读
  5. 嵌入式单片机项目开发的基本思想分享

    2024-07-13 16:20:06       17 阅读
  6. uni-app 蓝牙传输

    2024-07-13 16:20:06       17 阅读
  7. termux 安装 rockylinux

    2024-07-13 16:20:06       19 阅读