Table of contents
The FileReader object 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>
element, from a drag and drop operation's DataTransfer object, or from the mozGetAsFile() API on an HTMLCanvasElement
.
To create a FileReader, simply do the following:
var reader = new FileReader();
See Using files from web applications for details and examples.
Important note about input parameters
Note that in Gecko 1.9.2 (Firefox 3.6 / Thunderbird 3.1 / Fennec 1.0)
and early builds of Firefox 4, this interface used File
objects to specify files; the specification uses Blob objects. Starting in Gecko 2.0 (beta 7) (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)
, the implementation matches the specification. This may result in some changes being needed to your code, especially if you used the non-standard Mozilla-specific methods that have been deprecated on the File object.
Method overview
void abort(); |
void readAsArrayBuffer(in Blob blob); Requires Gecko 7.0
|
void readAsBinaryString(in Blob blob); |
void readAsDataURL(in Blob file); |
void readAsText(in Blob blob, [optional] in DOMString encoding); |
State constants
| Constant | Value | Description |
EMPTY | 0 | No data has been loaded yet. |
LOADING | 1 | Data is currently being loaded. |
DONE | 2 | The entire read request has been completed. |
Properties
| Property | Type | Description |
error | DOMError
| The error that occurred while reading the file. Read only. |
readyState | unsigned short | Indicates the state of the FileReader. This will be one of the State constants. Read only. |
result | jsval
| The file's contents. This property is only valid after the read operation is complete, and the format of the data depends on which of the methods was used to initiate the read operation. Read only. |
Methods
abort()
Aborts the read operation. Upon return, the readyState will be DONE.
void abort();
Parameters
None.
Exceptions thrown
DOM_FILE_ABORT_ERRabort()was called while no read operation was in progress (that is, the state wasn'tLOADING).Note: This exception was not thrown by Gecko until Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3) .
readAsArrayBuffer()
Starts reading the contents of the specified Blob
or File
. When the read operation is finished, the readyState will become DONE, and the onloadend callback, if any, will be called. At that time, the result attribute contains an ArrayBuffer representing the file's data.
void readAsArrayBuffer( in Blob blob );
Parameters
blob- The DOM
BloborFileto read into theArrayBuffer.
readAsBinaryString()
Starts reading the contents of the specified Blob
, which may be a File
. When the read operation is finished, the readyState will become DONE, and the onloadend callback, if any, will be called. At that time, the result attribute contains the raw binary data from the file.
void readAsBinaryString( in Blob blob );
readAsDataURL()
Starts reading the contents of the specified Blob
or File
. When the read operation is finished, the readyState will become DONE, and the onloadend callback, if any, will be called. At that time, the result attribute contains a data: URL representing the file's data.
void readAsDataURL( in Blob file );
This method is useful, for example, to get a preview of an image before uploading it:
readAsText()
Starts reading the specified blob's contents. When the read operation is finished, the readyState will become DONE, and the onloadend callback, if any, will be called. At that time, the result attribute contains the contents of the file as a text string.
void readAsText(
in Blob blob,
in DOMString encoding Optional
);
Event handlers
onabort- Called when the read operation is aborted.
onerror- Called when an error occurs.
onload- Called when the read operation is successfully completed.
onloadend- Called when the read is completed, whether successful or not. This is called after either
onloadoronerror. onloadstart- Called when reading the data is about to begin.
onprogress- Called periodically while the data is being read.
Browser compatibility
| Feature | Firefox (Gecko) | Chrome | Internet Explorer* | Opera* | Safari |
|---|---|---|---|---|---|
| Basic support | 3.6 (1.9.2) | 7 | 10 | -- | 5.1 |
| Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | ? | ? | ? | ? | ? |
*IE9 has a File API Lab. Opera has partial support in 11.10.
Gecko-specific notes
- Prior to Gecko 2.0 beta 7 (Firefox 4.0 beta 7), all
Blobparameters below wereFileparameters; this has since been updated to match the specification correctly. - Prior to Gecko 13.0 (Firefox 13.0 / Thunderbird 13.0)
the
FileReader.errorproperty returned aFileErrorobject. This interface has been removed andFileReader.erroris now returning theDOMErrorobject as defined in the latest FileAPI draft.


Mozilla Developer Network