I'm trying to convert PNG images, a lot of them to Base64 and then send them over a post request to my server-side application.
I have looked up and I found this function which author has done http://jsfiddle.net/handtrix/yvq5y/ here, that do exactly what I want:
function convertImgToBase64URL(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'), dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;}
I'm using it this way:
convertImgToBase64URL(model.cells[i].attrs.image["xlink:href"], function(base64Img){
// Base64DataURL
console.log(base64Img)
});
As you can see each cells contains an image, which I need to convert to base64 before sending it over a post request, my question is how can I convert an array of PNG images to Base64 and wait for them to finish and then send my array, I know that this is an ajax request and I can't do this synchronously as it's not very natural to javascript, but I can't seem to find a solution. How this is done properly?