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 in one of two ways: from a
FileList object returned as a result of a user selecting files using the
<input> element, or from a drag and drop operation's DataTransfer object.
To create a FileReader, simply do the following:
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
Prior to Gecko 2.0 beta 7 (Firefox 4.0 beta 7), all
Blob parameters below were
File parameters; this has since been updated to match the specification correctly.
void abort(); |
void readAsArrayBuffer(in Blob blob);
Unimplemented
|
void readAsBinaryString(in Blob blob); |
void readAsDataURL(in Blob blob); |
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 | FileError | The error that occurred while reading the file. Read only. |
readyState | integer | Indicates the state of the FileReader. This will be one of the State constants. Read only. |
result | string | 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.
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 blob );
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, [optiona] in DOMString encoding );
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 | ? | ? | Nightly build |
| Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | ? | ? | ? | ? | ? |

