I am working on an application in react that uses an api and to make a post we have to send an image in base64 . I was looking on stack overflow and saw this example:
function getBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
var file = document.querySelector('#files > input[type="file"]').files[0];
getBase64(file); // prints the base64 string
I'm confused to why I'd use new FileReader() when I could simply have one line of code that converts the string to base 64 using btoa()? Thank you in advance!
I can make this work either way I just don't know the point of using new FileReader() vs btoa.