前端方法 将base64格式转化为文件流并传给后端

  1. base64图片格式转化为文件流代码:
//data:base64图片格式字符串
//filename:文件名称
   base64toFile(data, fileName) {
          const dataArr = data.split(",");
          const byteString = atob(dataArr[1]);
          const options = {
            type: "image/jpeg",
            endings: "native"
          };
          const u8Arr = new Uint8Array(byteString.length);
          for (let i = 0; i < byteString.length; i++) {
            u8Arr[i] = byteString.charCodeAt(i);
          }
          return new File([u8Arr], fileName + ".jpg", options);//返回文件流
    },

  1. base64图片格式转换为文件流后,传送给后端
        let str = this.base64toFile(imageStr,'file')//base64图片格式转文件流

        let formData = new FormData();

        formData.append("file", str);
        //发送给后端
        uploadImg(formData).then(response => {
  

        }).catch(response => {
        
        })

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