Image to Base64
Image requires CORS Allow permission if not from the same domain. Should be OK for Phonegap if the domain is whitelisted.
JavaScript
var img = 'http://s3-ap-southeast-1.amazonaws.com/dino-devtest/necktie.jpg';
img = 'http://localhost:4460/ImageHand.aspx?ImageID=07170002811&Numero=0';
var oReq = new XMLHttpRequest();
oReq.open("GET", img, true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
var x = img.split('.');
var ext = x[x.length - 1];
var b64img = _arrayBufferToBase64(arrayBuffer);
$('<img />').attr('src', 'data:image/' + ext + ';base64,' + b64img).appendTo($('body'));
$('<textarea />').width(500).height(200).html(b64img).appendTo($('body'));
}
};
oReq.send(null);
function _arrayBufferToBase64(buffer) {
var binary = ''
var bytes = new Uint8Array(buffer)
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return window.btoa(binary);
}