Simple JavaScript code to convert a local image file to base64 - code can be used in ionic/angular project, simply call the function on some event
async uploadFiles() {
try {
let canvas = document.createElement('canvas');
let img = document.createElement('img');
img.src = "assets/icon/mserror.jpg";
img.onload = function () {
canvas.height = img.height;
canvas.width = img.width;
let dataURL = canvas.toDataURL('image/png');
console.log('>>><<<',dataURL);
canvas = null; ;
}
}catch (e) {
console.log('error while processing base64 conversion>>>', e)
}
}