I tried the same code with the code sample that you tried to do above, and this allowed me to load the local image file into base64 data and fill the image accordingly without errors. Try it...
<input id="txtFile" type="file" onchange="loadFile();"> <br> <img id="imgTest"> <script> function loadFile() { var file = document.getElementById('txtFile').files[0]; if (file) { var reader = new FileReader(); reader.onload = function(readerEvt) { var image = readerEvt.target.result; var base64image = image.split(',')[1]; console.log(readerEvt.target.result); document.getElementById('imgTest').src = readerEvt.target.result; }; reader.readAsDataURL(file); } else { console.log('Exception'); } } </script>
If it still does not work for you, there may be some problems with the image you are trying to upload.
source share