0

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.

Eunice Chia
  • 355
  • 1
  • 11

1 Answers1

0

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');
alert(getBase64Image(img));

// if the image isn't on the dom do this..
var img  = new Image();
img.onload = function(){
    alert(getBase64Image(img));
}
img.src= "http://my/image/url.png";
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116