【JS】base64转文件流再转blob

// img 是base64图片完整字符串
getUrl(img){
   let file = this.base64ImgtoFile(img); // 获取文件流
   let imgUrl = window.webkitURL.createObjectURL(file) ||
            window.URL.createObjectURL(file); // 获取链接
},
// 获取文件流
 base64ImgtoFile(dataurl, filename = "file") {
      const arr = dataurl.split(",");
      const mime = arr[0].match(/:(.*?);/)[1];
      const suffix = mime.split("/")[1];
      const bstr = atob(arr[1]);
      let n = bstr.length;
      const u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], `${filename}.${suffix}`, {
        type: mime,
      });
    },

你可能感兴趣的:(javascript,前端,开发语言)