I\'m trying to encode an image to base64 and send it to a server. When I retrieve the image all it shows is blank.
The code I\'m using to encode it is this:
I use the following code to convert an image to Base64:
var Base64 = {
/**
* This implementation relies on Cordova 1.5 or above implementations.
*/
getBase64ImageFromInput : function (input, callback) {
var imageReader = new FileReader();
imageReader.onloadend = function (evt) {
if (callback)
callback(evt.target.result);
};
//Start the asynchronous data read.
imageReader.readAsDataURL(input);
},
formatImageSrcString : function (base64) {
return (base64.match(/(base64)/))? base64 : "data:image/jpeg;base64," + base64;
}
};
Below is an example using this object to convert an image from a file input to base64 encoding:
var fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.onchange = function () {
var input = this.files[0];
if (input) {
Base64.getBase64ImageFromInput(input, function (imageData) {
//Process the image string.
console.log(imageData);
});
} else {
alert("Please select an image.");
}
};
Hope this helps. Let me know if you have any questions.