js 将图片转为base64格式
<img id="image" src='../../assets/xhs.jpeg'>
--------------------------------------------------------
getimgBase64 (img) { var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, img.width, img.height); var dataURL = canvas.toDataURL("image/jpeg"); canvas = null; //释放 dataURL = dataURL.replace("data:image/jpeg;base64,", ""); return dataURL; }, getresult () { let _ = this var image = document.getElementById('image'); image.onload = function () { var base64img = _.getimgBase64(image); //base64编码 console.log("base64img", base64img); } }, getresult ()