js把本地图片或项目内的图片转为base64编码

 const imgUrl="static/logo.png"
getBase64(imgUrl) {
     
      window.URL = window.URL || window.webkitURL;
      const xhr = new XMLHttpRequest();
      xhr.open("get", imgUrl, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status == 200) {
          //得到一个blob对象
          const blob = xhr.response;
          const oFileReader = new FileReader();
          oFileReader.readAsDataURL(blob);
          oFileReader.onloadend = e => {
            const base64 = e.target.result;//base64编码
           
          };
        }
      };
      xhr.send();
    },

 

你可能感兴趣的:(JS,vue,base64编码)