I'm basically trying to select an image via a HTML Input and then receive the image via Javascript and get its Base64/Image Data string.
However, the code I've used only returns half of it/partially/corrupt/invalid representation of the image. I tried converting the base64 string returned by it using an online tool and it just returned me a blank image (meaning: corrupt base64 representation)
Here's my Javascript code:
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = new Image;
img.src = URL.createObjectURL(e.target.files[0]);
img.onload = function () {
c.width = img.width;
c.height = img.height;
ctx.drawImage(img, 0, 0);
}
var imageData = c.toDataURL('image/png');
alert(imageData);
Here's a JSFiddle. http://jsfiddle.net/t2mcr3Lr/1/
As you can see, the base64 string doesn't seem right: as in, the string either seems partially formed or its invalid all together. I can't seem to convert it back to an image.
What am I doing wrong?