Just a few weeks until the 2021 JavaScript Full-Stack Bootcamp opens.
Signup to the waiting list!
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:
onload
triggered when the reading successfully endsonloadstart
triggered when the reading startsonprogress
triggered when the reading is on progressonloadend
triggered when the reading ends, successfully or not successfullyonerror
triggered when an error occursonabort
triggered 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 my free JavaScript Beginner's Handbook
The 2021 JavaScript Full-Stack Bootcamp will start at the end of March 2021. Don't miss this opportunity, signup to the waiting list!
More browser tutorials:
- Some useful tricks available in HTML5
- How I made a CMS-based website work offline
- The Complete Guide to Progressive Web Apps
- The Fetch API
- The Push API Guide
- The Channel Messaging API
- Service Workers Tutorial
- The Cache API Guide
- The Notification API Guide
- Dive into IndexedDB
- The Selectors API: querySelector and querySelectorAll
- Efficiently load JavaScript with defer and async
- The Document Object Model (DOM)
- The Web Storage API: local storage and session storage
- Learn how HTTP Cookies work
- The History API
- The WebP Image Format
- XMLHttpRequest (XHR)
- An in-depth SVG tutorial
- What are Data URLs
- Roadmap to learn the Web Platform
- CORS, Cross-Origin Resource Sharing
- Web Workers
- The requestAnimationFrame() guide
- What is the Doctype
- Working with the DevTools Console and the Console API
- The Speech Synthesis API
- How to wait for the DOM ready event in plain JavaScript
- How to add a class to a DOM element
- How to loop over DOM elements from querySelectorAll
- How to remove a class from a DOM element
- How to check if a DOM element has a class
- How to change a DOM node value
- How to add a click event to a list of DOM elements returned from querySelectorAll
- WebRTC, the Real Time Web API
- How to get the scroll position of an element in JavaScript
- How to replace a DOM element
- How to only accept images in an input file field
- Why use a preview version of a browser?
- The Blob Object
- The File Object
- The FileReader Object
- The FileList Object
- ArrayBuffer
- ArrayBufferView
- The URL Object
- Typed Arrays
- The DataView Object
- The BroadcastChannel API
- The Streams API
- The FormData Object
- The Navigator Object
- How to use the Geolocation API
- How to use getUserMedia()
- How to use the Drag and Drop API
- How to work with scrolling on Web Pages
- Handling forms in JavaScript
- Keyboard events
- Mouse events
- Touch events
- How to remove all children from a DOM element
- How to create an HTML attribute using vanilla Javascript
- How to check if a checkbox is checked using JavaScript?
- How to copy to the clipboard using JavaScript
- How to disable a button using JavaScript
- How to make a page editable in the browser
- How to get query string values in JavaScript with URLSearchParams
- How to remove all CSS from a page at once
- How to use insertAdjacentHTML
- Safari, warn before quitting
- How to add an image to the DOM using JavaScript
- How to reset a form
- How to use Google Fonts