File API
Read files using File API
IntermediateTopic: Browser API
JavaScript File API Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (e) => console.log('File content:', e.target.result);
reader.readAsText(file);
});Output
File content: Hello World
Understanding File API
File API reads file contents.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.