I need to create the ability to attach pictures and send them via JSON in base64.
But I can't get the resulting string from FileReader object. I will be very grateful if you can tell me how to fix this.
<form>
<label>Picture
<input type="file" accept=".jpg, .jpeg, .png" name="picture" required>
</label>
</form>
async function push() {
// some other actions here
let form = new FormData(document.querySelector('form'));
let json = construct_json(form);
//calls of other functions
}
function getBase64(file) {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () { // I think there is a mistake here.
reader.result;
};
}
function construct_json(form) {
let json = {
picture: getBase64(form.get('picture')),
};
return JSON.stringify(json);
}
UPD: If I'm trying to use json in the push() function then I get the same problem. And adding await doesn't help. I would be grateful for a hint how can I display json in the push() function?