【JS】convert base64 to file stream and then to 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,
      });
    },

Guess you like

Origin blog.csdn.net/qq_43585322/article/details/129690461