The FileReader Object
Find out what is a FileReader object and how to use it
The FileReader object asynchronously reads the content of a file.
It exposes those 4 reading methods we can use to start a reading process:
readAsText()readAsDataURL()readAsArrayBuffer()readAsBinaryString()
and an abort() method to halt any reading operation.
Reading the file is asynchronous, and the object exposes several events we can hook into to follow the progress of the operation:
onloadtriggered when the reading successfully endsonloadstarttriggered when the reading startsonprogresstriggered when the reading is on progressonloadendtriggered when the reading ends, successfully or not successfullyonerrortriggered when an error occursonaborttriggered when the reading is aborted
Once a reading operation is completed, the result property of FileReader contains the file content.
The error property contains the error message, if an error occurred, and readyState contains the state of the operations - 0 if no data is loaded, 1 if data loading is in progress, and 2 if the loading has finished.
readAsText()
Loads the content of a blob in a text string.
In this example we use that text and put it into the #content element’s inner HTML:
//..file is available as a blob
const reader = new FileReader()
reader.onload = (event) => {
const text = reader.result
document.getElementById('content').innerHTML = text
}
reader.onerror = (e) => {
console.error(e)
}
reader.readAsText(file)
Here’s an example that reads the content of a text file when it’s uploaded using an input element, and prints its content to the console:
<input type="file" allow="text/*" />
const input = document.querySelector('input')
input.addEventListener('change', (e) => {
const reader = new FileReader()
reader.onload = (event) => {
const text = reader.result
console.log(text)
}
reader.onerror = (e) => {
console.error(e)
}
reader.readAsText(input.files[0])
})
readAsDataURL()
Loads the content of a blob into a Data URL.
//..file is available as a blob
const reader = new FileReader()
reader.onload = (event) => {
const dataURL = event.target.result
document.getElementById('image').src = dataURL
}
reader.readAsDataURL(file)
readAsArrayBuffer()
Loads the content of a blob into an ArrayBuffer.
//..file is available as a blob
const reader = new FileReader()
reader.onload = (event) => {
const buffer = reader.result
const data = new Int32Array(buffer)
//...
}
reader.onerror = (e) => {
console.error(e)
}
reader.readAsArrayBuffer(file) download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.