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.
Asked
Active
Viewed 469 times
0

Eunice Chia
- 355
- 1
- 11
-
2That will be server-side code, not client-side. What are you actually trying to do? – Reinstate Monica Cellio Aug 08 '16 at 14:33
-
try to take a look to http://stackoverflow.com/questions/19124701/get-image-using-jquery-ajax-and-decode-it-to-base64/25371174#25371174 – gaetanoM Aug 08 '16 at 14:45
1 Answers
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