Looking for a way to encode and send images via JSON using base64, but struggling with getting the result from the FileReader object. Any assistance would be highly appreciated.
<form>
<label>Picture
<input type="file" accept=".jpg, .jpeg, .png" name="picture" required>
</label>
</form>
async function push() {
// additional 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 () { // suspected issue here
reader.result;
};
}
function construct_json(form) {
let json = {
picture: getBase64(form.get('picture')),
};
return JSON.stringify(json);
}
UPDATE: Encountering the same problem when trying to use JSON in the push() function, even adding await does not solve it. Suggestions on how to display JSON in the push() function would be greatly welcomed.