js 图片转base64
let canvas = document.createElement("canvas"); let img = document.getElementById('img'); canvas.width = img.width; canvas.height = img.height; let ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, img.width, img.height); let dataURL = canvas.toDataURL("image/png"); console.log(dataURL); // 结果
注: 不能使用本地图片,存在跨域问题