Eunice Chia Eunice Chia - 7 months ago 40
jQuery Question

get base64 by filename to view

I saved name of my files into the db, and store the file in file server. So I can access the file by getting the name and do

<img src='http://localhost/upload/abc.jpg'/>
where abc is the filename. But how do I get base64? because I need to get the actual file and pass it to somewhere.


If the image is on the same domain, you can do it with a 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 the image is on the dom use this
var img = document.getElementById('img');

// if the image isn't on the dom do this..
var img  = new Image();
img.onload = function(){
img.src= "http://my/image/url.png";