FileReader

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:

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

Gecko 1.9.2 note
(Firefox 3.6 / Thunderbird 3.1 / Fennec 1.0)

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 in Firefox 4.0
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.

Exceptions thrown
DOM_FILE_ABORT_ERR
abort() was called while no read operation was in progress (that is, the state wasn't LOADING).
Remarks

The DOM_FILE_ABORT_ERR exception was not thrown by Gecko until Gecko 6.0 (Firefox 6.0) .

readAsArrayBuffer()

Unimplemented in Firefox 4.0

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 Blob or File to read into the ArrayBuffer.

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
);
Parameters
blob
The DOM Blob or File from which to read.

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
);
Parameters
blob
The DOM Blob or File from which to read.

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
);
Parameters
blob
The DOM Blob or File from which to read.
encoding Optional
A string indicating the encoding to use for the returned data. By default, UTF-8 is assumed if this parameter isn't specified.

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 onload or onerror.
onloadstart
Called when reading the data is about to begin.
onprogress
Called periodically while the data is being read.

Browser compatibility

  • Desktop
  • Mobile
Feature Firefox (Gecko) Chrome Internet Explorer* Opera* Safari
Basic support 3.6 (1.9.2) 7 -- -- Nightly build

*IE9 has a File API Lab. Opera has partial support in 11.10.

Tags (3)

Attachments (0)

 

Attach file