Read file as string
suggest changeMake 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]);
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents