FileReader

  • Revision slug: DOM/FileReader
  • Revision title: FileReader
  • Revision id: 3690
  • Created:
  • Creator: ebidel
  • Is current revision? No
  • Comment 1 words added, 6 words removed

Revision Content

{{ gecko_minversion_header("1.9.2") }}

The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using {{ domxref("File") }} or {{ domxref("Blob") }} objects to specify the file or data to read. File objects may be obtained in one of two ways: from a {{ domxref("FileList") }} object returned as a result of a user selecting files using the {{ HTMLElement("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 {{ geckoRelease("1.9.2") }} and early builds of Firefox 4, this interface used {{ domxref("File") }} objects to specify files; the specification uses Blob objects. Starting in Gecko 2.0 (beta 7) {{ geckoRelease("2.0") }}, 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_callout_heading("1.9.2") }}

Prior to Gecko 2.0 beta 7 (Firefox 4.0 beta 7), all {{ domxref("Blob") }} parameters below were {{ domxref("File") }} parameters; this has since been updated to match the specification correctly.

void abort();
void readAsArrayBuffer(in Blob blob); {{ unimplemented_inline() }} 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 {{ anch("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.

readAsArrayBuffer()

{{ unimplemented_inline() }} in Firefox 4.0

Starts reading the contents of the specified {{ domxref("Blob") }} or {{ domxref("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
);

readAsBinaryString()

Starts reading the contents of the specified {{ domxref("Blob") }}, which may be a {{ domxref("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 {{ domxref("Blob") }} or {{ domxref("File") }} from which to read.

readAsDataURL()

Starts reading the contents of the specified {{ domxref("Blob") }} or {{ domxref("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 {{ domxref("Blob") }} or {{ domxref("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 {{ domxref("Blob") }} or {{ domxref("File") }} from which to read.
encoding {{ optional_inline() }}
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

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer* Opera* Safari
Basic support {{ CompatGeckoDesktop("1.9.2") }} 7 {{ CompatNo() }} {{ CompatNo() }} {{ CompatNightly("safari") }}
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

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

See also

Revision Source

<p>{{ gecko_minversion_header("1.9.2") }}</p>
<p>The <code>FileReader</code> object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using {{ domxref("File") }} or {{ domxref("Blob") }} objects to specify the file or data to read. File objects may be obtained in one of two ways: from a {{ domxref("FileList") }} object returned as a result of a user selecting files using the {{ HTMLElement("input") }} element, or from a drag and drop operation's <a href="/En/DragDrop/DataTransfer" title="En/DragDrop/DataTransfer"><code>DataTransfer</code></a> object.</p>
<p>To create a <code>FileReader</code>, simply do the following:</p>
<pre>reader = new FileReader();
</pre>
<p>See <a href="/en/Using_files_from_web_applications" title="en/Using files from web applications">Using files from web applications</a> for details and examples.</p>
<h2>Important note about input parameters</h2>
<p>Note that in Gecko 1.9.2 {{ geckoRelease("1.9.2") }} and early builds of Firefox 4, this interface used {{ domxref("File") }} objects to specify files; the specification uses <code>Blob</code> objects. Starting in Gecko 2.0 (beta 7) {{ geckoRelease("2.0") }}, 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 <code>File</code> object.</p>
<h2 name="Method_overview">Method overview</h2>
<div class="geckoVersionNote">
<p>{{ gecko_callout_heading("1.9.2") }}</p>
<p>Prior to Gecko 2.0 beta 7 (Firefox 4.0 beta 7), all {{ domxref("Blob") }} parameters below were {{ domxref("File") }} parameters; this has since been updated to match the specification correctly.</p>
</div>
<table class="standard-table"> <tbody> <tr> <td><code>void <a href="/en/DOM/FileReader#abort()" title="en/DOM/FileReader#abort()">abort</a>();</code></td> </tr> <tr> <td><code>void <a href="/en/DOM/FileReader#readAsArrayBuffer()" title="en/DOM/FileReader#readAsArrayBuffer()">readAsArrayBuffer</a>(in Blob blob);</code> {{ unimplemented_inline() }} in Firefox 4.0</td> </tr> <tr> <td><code>void <a href="/en/DOM/FileReader#readAsBinaryString()" title="en/DOM/FileReader#readAsBinaryString()">readAsBinaryString</a>(</code><code>in Blob blob</code><code>);<br> </code></td> </tr> <tr> <td><code>void <a href="/en/DOM/FileReader#readAsDataURL()" title="en/DOM/FileReader#readAsDataURL()">readAsDataURL</a>(</code><code>in Blob blob</code><code>);<br> </code></td> </tr> <tr> <td><code>void <a href="/en/DOM/FileReader#readAsText()" title="en/DOM/FileReader#readAsText()">readAsText</a>(</code><code>in Blob blob</code><code>, [optional] in DOMString encoding</code><code>);<br> </code></td> </tr> </tbody>
</table>
<h2 name="Attributes">State constants</h2>
<table class="standard-table"> <tbody> <tr> <td class="header">Constant</td> <td class="header">Value</td> <td class="header">Description</td> </tr> <tr> <td><code>EMPTY</code></td> <td>0</td> <td>No data has been loaded yet.</td> </tr> <tr> <td><code>LOADING</code></td> <td>1</td> <td>Data is currently being loaded.</td> </tr> <tr> <td><code>DONE</code></td> <td>2</td> <td>The entire read request has been completed.</td> </tr> </tbody>
</table>
<h2 name="Attributes">Properties</h2>
<table class="standard-table"> <tbody> <tr> <td class="header">Property</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>error</code></td> <td><a href="/en/DOM/FileError" title="en/DOM/FileError"><code>FileError</code></a></td> <td>The error that occurred while reading the file. <strong>Read only</strong>.</td> </tr> <tr> <td><code>readyState</code></td> <td><code>integer<br> </code></td> <td>Indicates the state of the <code>FileReader</code>. This will be one of the {{ anch("State constants") }}. <strong>Read only</strong>.</td> </tr> <tr> <td><code>result</code></td> <td><code>string</code></td> <td>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. <strong>Read only</strong>.</td> </tr> </tbody>
</table>
<h2 name="Methods">Methods</h2>
<h3 name="getAsBinary.28.29">abort()</h3>
<p>Aborts the read operation. Upon return, the <code>readyState</code> will be <code>DONE</code>.</p>
<pre>void abort();
</pre>
<h6>Parameters</h6>
<p>None.</p>
<h3 name="getAsArrayBuffer.28.29">readAsArrayBuffer()</h3>
<p>{{ unimplemented_inline() }} in Firefox 4.0</p>
<p>Starts reading the contents of the specified {{ domxref("Blob") }} or {{ domxref("File") }}. When the read operation is finished, the <code>readyState</code> will become <code>DONE</code>, and the <code>onloadend</code> callback, if any, will be called. At that time, the <code>result</code> attribute contains an <a href="/en/JavaScript_typed_arrays/ArrayBuffer" title="https://developer.mozilla.org/en/JavaScript_typed_arrays/ArrayBuffer">ArrayBuffer</a> representing the file's data.</p>
<pre class="eval">void readAsArrayBuffer(
  in Blob blob
);</pre>
<h3 name="getAsBinary.28.29">readAsBinaryString()</h3>
<p>Starts reading the contents of the specified {{ domxref("Blob") }}, which may be a {{ domxref("File") }}. When the read operation is finished, the <code>readyState</code> will become <code>DONE</code>, and the <code>onloadend</code> callback, if any, will be called. At that time, the <code>result</code> attribute contains the raw binary data from the file.</p>
<pre class="eval">void readAsBinaryString(
  in Blob blob
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>blob</code></dt> <dd>The DOM {{ domxref("Blob") }} or {{ domxref("File") }} from which to read.</dd>
</dl>
<h3 name="getAsDataURL.28.29">readAsDataURL()</h3>
<p>Starts reading the contents of the specified {{ domxref("Blob") }} or {{ domxref("File") }}. When the read operation is finished, the <code>readyState</code> will become <code>DONE</code>, and the <code>onloadend</code> callback, if any, will be called. At that time, the <code>result</code> attribute contains a <code>data:</code> URL representing the file's data.</p>
<pre class="eval">void readAsDataURL(
  in Blob blob
);</pre>
<h6 name="Parameters_2">Parameters</h6>
<dl> <dt><code>blob<br> </code></dt> <dd>The DOM {{ domxref("Blob") }} or {{ domxref("File") }} from which to read.</dd>
</dl>
<h3 name="getAsText.28.29">readAsText()</h3>
<p>Starts reading the specified blob's contents. When the read operation is finished, the <code>readyState</code> will become <code>DONE</code>, and the <code>onloadend</code> callback, if any, will be called. At that time, the <code>result</code> attribute contains the contents of the file as a text string.</p>
<pre class="eval">void readAsText(
  in Blob blob,
  [optiona] in DOMString encoding
);
</pre>
<h6 name="Parameters_3">Parameters</h6>
<dl> <dt> </dt><dt><code>blob<br> </code></dt> <dd>The DOM {{ domxref("Blob") }} or {{ domxref("File") }} from which to read.</dd> <dt><code>encoding</code> {{ optional_inline() }}</dt> <dd>A string indicating the encoding to use for the returned data. By default, UTF-8 is assumed if this parameter isn't specified.</dd>
</dl>
<h2>Event handlers</h2>
<dl> <dt><code>onabort</code></dt> <dd>Called when the read operation is aborted.</dd> <dt><code>onerror</code></dt> <dd>Called when an error occurs.</dd> <dt><code>onload</code></dt> <dd>Called when the read operation is successfully completed.</dd> <dt><code>onloadend</code></dt> <dd>Called when the read is completed, whether successful or not. This is called after either <code>onload</code> or <code>onerror</code>.</dd> <dt><code>onloadstart</code></dt> <dd>Called when reading the data is about to begin.</dd> <dt><code>onprogress</code></dt> <dd>Called periodically while the data is being read.</dd>
</dl>
<h2>Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox (Gecko)</th> <th>Chrome</th> <th>Internet Explorer*</th> <th>Opera*</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatGeckoDesktop("1.9.2") }}</td> <td>7</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNightly("safari") }}</td> </tr> </tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox Mobile (Gecko)</th> <th>Android</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody>
</table>
</div>
<p>*IE9 has a <a class=" external" href="http://html5labs.interoperabilitybridges.com/prototypes/fileapi/fileapi/info" title="http://html5labs.interoperabilitybridges.com/prototypes/fileapi/fileapi/info">File API Lab</a>. Opera has <a class=" external" href="http://my.opera.com/desktopteam/blog/2011/04/05/stability-gmail-socks" title="http://my.opera.com/desktopteam/blog/2011/04/05/stability-gmail-socks">partial support</a> in 11.10.</p>
<h2>See also</h2>
<ul> <li><a href="/en/Using_files_from_web_applications" title="en/Using files from web applications">Using files from web applications</a></li> <li><a href="/en/DOM/File" title="en/DOM/File"><code>File</code></a></li> <li>{{ spec("http://www.w3.org/TR/FileAPI/#FileReader-interface", "Specification: File API: FileReader", "WD") }}</li>
</ul>
Revert to this revision