Read local file

Select text or image


Sample code
function fileSelect(t) { //target is the button
console.assert(t == button);
const f = t.files[0];
const size = " "+f.size+" bytes ";
msg.innerText = f.type+size;
if (f.type.startsWith("image")) {
reader.onload = function(evt) {
const a = evt.target.result; //image source
out.innerHTML = "<img class=thumb "
+"src='"+a+"' title='"+f.name+"' />";
console.log(f.name+size+f.type);
};
reader.readAsDataURL(f);
} else if (isText(f)) {
reader.onload = function(evt) { //text
const a = evt.target.result.replace(/</g, "&lt;");
out.innerHTML = "<pre>"+a+"</pre>";
console.log(f.name+size+f.type);
};
reader.readAsText(f);
} else {
msg.innerText += "Unknown ";
out.innerText = "";
}
}

Ref: File selection