javascript - get base64 by filename to view -
i saved name of files db, , store file in file server. can access file getting name , <img src='http://localhost/upload/abc.jpg'/>
abc filename. how base64? because need actual file , pass somewhere.
if image on same domain, can canvas..
function 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); return canvas.todataurl("image/png"); } // if image on dom use var img = document.getelementbyid('img'); alert(getbase64image(img)); // if image isn't on dom this.. var img = new image(); img.onload = function(){ alert(getbase64image(img)); } img.src= "http://my/image/url.png";
Comments
Post a Comment