The
FileReader
interface lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using
File
or
Blob
objects to specify the file or data to read.
File objects may be obtained from a
FileList
object returned as a result of a user selecting files using the
<input type="file">
element, or from a drag and drop operation's
DataTransfer
object.
FileReader
can only access the contents of files that the user has explicitly selected; it cannot be used to read a file by pathname from the user's file system. To read files on the client's file system by pathname, use the
File System Access API
. To read server-side files, use
fetch()
, with
CORS
permission if reading cross-origin.
Starts reading the contents of the specified
Blob
, once finished, the
result
attribute contains the contents of the file as a text string. An optional encoding name can be specified.
Listen to these events using
addEventListener()
or by assigning an event listener to the
oneventname
property of this interface. Remove the event listeners with
removeEventListener()
, once
FileReader
is no longer used, to avoid memory leaks.