The WebPlatform project has been discontinued when the Stewards partnership agreement ended in September 2015. This site has been frozen, and the assets we have created will remain.


< apis‎ | file
Jump to: navigation, search


This article is Ready to Use.

W3C Last Call Working Draft


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.


No properties.


API Name Summary
readAsDataURL Returns the complete data of blob as a Data URL, essentially a Base64-encoded string of the file data.
readAsText Returns partial Blob data representing the number of bytes currently loaded (as a fraction of the total), decoded into memory according to the encoding determination.


No events.


Preview an image before upload


<!doctype html>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Image preview example</title>
<script type="text/javascript">
oFReader = new FileReader(), rFilter = 'image/jpg';
oFReader.onload = function (oFREvent) {
  document.getElementById("uploadPreview").src =;
function loadImageFile() {
  if (document.getElementById("uploadImage").files.length === 0) { return; }
  var oFile = document.getElementById("uploadImage").files[0];
  if (!rFilter.test(oFile.type)) { alert("You must select a valid image file!"); return; }
<body onload="loadImageFile();">
  <form name="uploadForm">
          <td><img id="uploadPreview" style="width: 100px; height: 100px;" src="" alt="Image preview" /></td>
          <td><input id="uploadImage" type="file" name="myPhoto" onchange="loadImageFile();" /></td>
    <p><input type="submit" value="Send" /></p>

View live example


When the FileReader constructor is invoked, a new FileReader object is returned. This FileReader object enables asynchronous reads on individual File objects by firing progress events as the read occurs to event handler methods attached to the FileReader object.

Related specifications

Specification Status Related Changes
W3C File API Specification W3C Working Draft

See also

Other articles

Using files from web applications


This article contains content originally from external sources.

Portions of this content come from the Mozilla Developer Network cc-by-sa-small-wpd.svg: [FileReader Article]

Portions of this content come from the Microsoft Developer Network: [fileReader Object Article]