base64格式的图片转成文件格式的图片并上传到服务器

function convertBase64UrlToBlob(urlData) {
  let bytes = window.atob(urlData.split(",")[1]); //去掉url的头,并转换为byte
  //处理异常,将ascii码小于0的转换为大于0
  let ab = new ArrayBuffer(bytes.length);
  let ia = new Uint8Array(ab);
  for (var i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i);
  }

  return new Blob([ab], {
    type: "image/jpg",
  });
}



        let formdata = new FormData();
        formdata.append("pic_type", 3);
        console.log(formdata);
        console.log("filename", blobInfo.filename());
        const filename = blobInfo.filename();
        let imgeFile = convertBase64UrlToBlob(
            "data:image/png;base64," + blobInfo.base64()
        );
        formdata.append("file", imgeFile, new Date().getTime() + ".jpg");

        PictureService.upload(formdata)
          .then((res) => {
            console.log(res);
            if (res.code === 200) {
              resolve(res.data.file_id);
            }
          })
          .catch((err) => {
            reject(err);
          });
      });

猜你喜欢

转载自blog.csdn.net/xutongbao/article/details/125379246