JSFiddle - React, Tailwind, and code Playground

by Alexandru Gatea

HTML

<input type="file" id="file">

JavaScript

const i = document.getElementById("file");


i.addEventListener("change", ()=> {
	const a = i.value;
	
	let file = i.value;
	getBase64(file); // prints the base64 string
	
	//localStorage.setItem("img", imgData);
});


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);
   };
}