I'm trying to upload an image with FileReader() and convert it to base64. This only works when after I upload the image twice, otherwise, a blank canvas/image gets posted instead.
This is my input:
<input type="file" id="file" class="file"/><canvas id="canvas"></canvas>
This is the FileReader():
var file = document.querySelector('input[type=file]').files[0];
var fr = new FileReader();
fr.onload = createImage; // onload fires after reading is complete
fr.readAsDataURL(file); // begin reading
function createImage() {
img = new Image();
img.onload = imageLoaded;
img.src = fr.result;
}
function imageLoaded() {
var canvas = document.getElementById("canvas")
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img,0,0);
}
var img64 = canvas.toDataURL().split(",")[1];
var params = {
"media_data": img64
};