FileReader

FileReader is an object that allows one to read a file.

Properties

  • readyState: One of the three states the reader can be in EMPTY, LOADING or DONE.
  • result: The contents of the file that has been read. (DOMString)
  • error: An object containing errors. (FileError)
  • onloadstart: Called when the read starts. . (Function)
  • onprogress: Called while reading the file, reports progress (progess.loaded/progress.total). (Function) -NOT SUPPORTED
  • onload: Called when the read has successfully completed. (Function)
  • onabort: Called when the read has been aborted. For instance, by invoking the abort() method. (Function)
  • onerror: Called when the read has failed. (Function)
  • onloadend: Called when the request has completed (either in success or failure). (Function)

Methods

  • abort: Aborts reading file.
  • readAsDataURL: Read file and return data as a base64 encoded data url.
  • readAsText: Reads text file.
  • readAsBinaryString: Reads file as binary and returns a binary string.
  • readAsArrayBuffer: Reads file as an ArrayBuffer.

Details

The FileReader object is a way to read files from the devices file system. Files can be read as text or as a base64 data encoded string. Users register their own event listeners to receive the loadstart, progress, load, loadend, error and abort events.

Supported Platforms

  • Android
  • BlackBerry WebWorks (OS 5.0 and higher)
  • iOS
  • Windows Phone 7 and 8
  • Windows 8

Read As Data URL

Parameters:

  • file - the file object to read

Quick Example

function win(file) {
	var reader = new FileReader();
	reader.onloadend = function(evt) {
    	console.log("read success");
        console.log(evt.target.result);
    };
	reader.readAsDataURL(file);
};

var fail = function(evt) {
	console.log(error.code);
};

entry.file(win, fail);

Read As Text

Parameters:

  • file - the file object to read
  • encoding - the encoding to use to encode the file's content. Default is UTF8.

Quick Example

function win(file) {
	var reader = new FileReader();
	reader.onloadend = function(evt) {
    	console.log("read success");
        console.log(evt.target.result);
    };
	reader.readAsText(file);
};

var fail = function(evt) {
	console.log(error.code);
};

entry.file(win, fail);

Abort Quick Example

function win(file) {
	var reader = new FileReader();
	reader.onloadend = function(evt) {
    	console.log("read success");
        console.log(evt.target.result);
    };
	reader.readAsText(file);
	reader.abort();
};

function fail(error) {
	console.log(error.code);
}

entry.file(win, fail);

Full Example

<!DOCTYPE html>
<html>
  <head>
    <title>FileReader Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // Cordova is ready
    //
    function onDeviceReady() {
		window.requestFileSystem([LocalFileSystem](../localfilesystem/localfilesystem.html).PERSISTENT, 0, gotFS, fail);
    }
	
	function gotFS(fileSystem) {
		fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
	}
	
	function gotFileEntry(fileEntry) {
		fileEntry.file(gotFile, fail);
	}
	
    function gotFile(file){
		readDataUrl(file);
		readAsText(file);
	}
    
    function readDataUrl(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as data URL");
            console.log(evt.target.result);
        };
        reader.readAsDataURL(file);
    }
    
    function readAsText(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as text");
            console.log(evt.target.result);
        };
        reader.readAsText(file);
    }
    
    function fail(evt) {
        console.log(evt.target.error.code);
    }
    
    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Read File</p>
  </body>
</html>

iOS Quirks

  • encoding parameter is not supported, UTF8 encoding is always used.

Read As Binary String

Currently supported on iOS and Android only.

Parameters:

  • file - the file object to read

Quick Example

function win(file) {
	var reader = new FileReader();
	reader.onloadend = function(evt) {
    	console.log("read success");
        console.log(evt.target.result);
    };
	reader.readAsBinaryString(file);
};

var fail = function(evt) {
	console.log(error.code);
};

entry.file(win, fail);

Read As Array Buffer

Currently supported on iOS and Android only.

Parameters:

  • file - the file object to read

Quick Example

function win(file) {
	var reader = new FileReader();
	reader.onloadend = function(evt) {
    	console.log("read success");
        console.log(new Uint8Array(evt.target.result));
    };
	reader.readAsArrayBuffer(file);
};

var fail = function(evt) {
	console.log(error.code);
};

entry.file(win, fail);