Depending on which browsers you are planning to support, you could consider using the showOpenFilePicker
API.
Example: https://rightful-serious-cost.glitch.me/
<textarea></textarea>
<button>Open file</button>
async function handleOpenFile() {
const [fileHandle] = await window.showOpenFilePicker({
types: [
{
description: 'JSON document',
accept: {
'text/*': ['.json'],
},
},
],
multiple: false,
});
const file = await fileHandle.getFile();
const contents = await file.text();
console.log(contents);
document.querySelector('textarea').textContent = contents;
}
document.querySelector('button').addEventListener('click', () => handleOpenFile());
Source code: https://glitch.com/edit/#!/rightful-serious-cost?path=index.html%3A13%3A6