美文网首页
文件base64 blob file互转

文件base64 blob file互转

作者: 芸芸众生ing | 来源:发表于2020-07-09 16:31 被阅读0次
   to(file, type, toType) {
      return Promise(Response => {
        if (type == "base64" && toType == "blob") {
          let arr = file.split(",");
          let img = window.atob(arr[1]);
          let mime = arr[0].match(/:(.*?);/)[1];
          let ia = new Uint8Array(img.length);
          for (var i = 0; i < img.length; i++) {
            ia[i] = img.charCodeAt(i);
          }
          Response(new Blob([ia], { type: mime }));
        }

        if (type == "blob" && toType == "file") {
          Response(new File([file], Math.floor(Math.random() * 900) + ".jpeg"));
        }

        if (type == "file" && toType == "base64") {
          var reader = new FileReader();
          reader.onload = function() {
            Response(this.result);
          };
          reader.readAsDataURL(file);
        }
      });
    },

相关文章

网友评论

      本文标题:文件base64 blob file互转

      本文链接:https://www.haomeiwen.com/subject/xgsscktx.html