// 图片文件转base64,利用canvasfunction getBase64Image(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 ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();var dataURL = canvas.toDataURL("image/"+ext);return dataURL;}// 使用var image = new Image();image.src = "${ctxStatic}/modules/cms/front/site/suzhou/img/login-bg.jpg"; //你的图片路径image.onload = function(){var imgBase64 = getBase64Image(image);console.log(imgBase64);}
测试结果如图所示:
具体详情可以参考以下博客
/tags/Ntjacg5sODkwMzMtYmxvZwO0O0OO0O0O.html