Read file as string

suggest change

Make sure to have a file input on your page:

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

FileReader is a browser API. readAsText decodes the file as UTF-8 unless a second encoding argument is passed. The listener is a normal function, so this is the input element:

document.getElementById('upload').addEventListener('change', readFileAsString)
function readFileAsString() {
    const files = this.files;
    if (files.length === 0) {
        console.log('No file is selected');
        return;
    }

    const reader = new FileReader();
    reader.onload = function(event) {
        console.log('File content:', event.target.result);
    };
    reader.readAsText(files[0]);
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents