blob: b740635cde5049d7827d327ae42d817804d75bf0 [file] [log] [blame]
var exec = require('cordova/exec'),
FileError = require('cordova/plugin/FileError'),
ProgressEvent = require('cordova/plugin/ProgressEvent');
/**
* This class reads the mobile device file system.
*
* For Android:
* The root directory is the root of the file system.
* To read from the SD card, the file name is "sdcard/my_file.txt"
* @constructor
*/
var FileReader = function() {
this.fileName = "";
this.readyState = 0; // FileReader.EMPTY
// File data
this.result = null;
// Error
this.error = null;
// Event handlers
this.onloadstart = null; // When the read starts.
this.onprogress = null; // While reading (and decoding) file or fileBlob data, and reporting partial file data (progess.loaded/progress.total)
this.onload = null; // When the read has successfully completed.
this.onerror = null; // When the read has failed (see errors).
this.onloadend = null; // When the request has completed (either in success or failure).
this.onabort = null; // When the read has been aborted. For instance, by invoking the abort() method.
};
// States
FileReader.EMPTY = 0;
FileReader.LOADING = 1;
FileReader.DONE = 2;
/**
* Abort reading file.
*/
FileReader.prototype.abort = function() {
this.result = null;
if (this.readyState == FileReader.DONE || this.readyState == FileReader.EMPTY) {
return;
}
this.readyState = FileReader.DONE;
// If abort callback
if (typeof this.onabort === 'function') {
this.onabort(new ProgressEvent('abort', {target:this}));
}
// If load end callback
if (typeof this.onloadend === 'function') {
this.onloadend(new ProgressEvent('loadend', {target:this}));
}
};
/**
* Read text file.
*
* @param file {File} File object containing file properties
* @param encoding [Optional] (see http://www.iana.org/assignments/character-sets)
*/
FileReader.prototype.readAsText = function(file, encoding) {
// Figure out pathing
this.fileName = '';
if (typeof file.fullPath === 'undefined') {
this.fileName = file;
} else {
this.fileName = file.fullPath;
}
// Already loading something
if (this.readyState == FileReader.LOADING) {
throw new FileError(FileError.INVALID_STATE_ERR);
}
// LOADING state
this.readyState = FileReader.LOADING;
// If loadstart callback
if (typeof this.onloadstart === "function") {
this.onloadstart(new ProgressEvent("loadstart", {target:this}));
}
// Default encoding is UTF-8
var enc = encoding ? encoding : "UTF-8";
var me = this;
// Read file
exec(
// Success callback
function(r) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileReader.DONE) {
return;
}
// Save result
me.result = r;
// If onload callback
if (typeof me.onload === "function") {
me.onload(new ProgressEvent("load", {target:me}));
}
// DONE state
me.readyState = FileReader.DONE;
// If onloadend callback
if (typeof me.onloadend === "function") {
me.onloadend(new ProgressEvent("loadend", {target:me}));
}
},
// Error callback
function(e) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileReader.DONE) {
return;
}
// DONE state
me.readyState = FileReader.DONE;
// null result
me.result = null;
// Save error
me.error = new FileError(e);
// If onerror callback
if (typeof me.onerror === "function") {
me.onerror(new ProgressEvent("error", {target:me}));
}
// If onloadend callback
if (typeof me.onloadend === "function") {
me.onloadend(new ProgressEvent("loadend", {target:me}));
}
}, "File", "readAsText", [this.fileName, enc]);
};
/**
* Read file and return data as a base64 encoded data url.
* A data url is of the form:
* data:[<mediatype>][;base64],<data>
*
* @param file {File} File object containing file properties
*/
FileReader.prototype.readAsDataURL = function(file) {
this.fileName = "";
if (typeof file.fullPath === "undefined") {
this.fileName = file;
} else {
this.fileName = file.fullPath;
}
// Already loading something
if (this.readyState == FileReader.LOADING) {
throw new FileError(FileError.INVALID_STATE_ERR);
}
// LOADING state
this.readyState = FileReader.LOADING;
// If loadstart callback
if (typeof this.onloadstart === "function") {
this.onloadstart(new ProgressEvent("loadstart", {target:this}));
}
var me = this;
// Read file
exec(
// Success callback
function(r) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileReader.DONE) {
return;
}
// DONE state
me.readyState = FileReader.DONE;
// Save result
me.result = r;
// If onload callback
if (typeof me.onload === "function") {
me.onload(new ProgressEvent("load", {target:me}));
}
// If onloadend callback
if (typeof me.onloadend === "function") {
me.onloadend(new ProgressEvent("loadend", {target:me}));
}
},
// Error callback
function(e) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileReader.DONE) {
return;
}
// DONE state
me.readyState = FileReader.DONE;
me.result = null;
// Save error
me.error = new FileError(e);
// If onerror callback
if (typeof me.onerror === "function") {
me.onerror(new ProgressEvent("error", {target:me}));
}
// If onloadend callback
if (typeof me.onloadend === "function") {
me.onloadend(new ProgressEvent("loadend", {target:me}));
}
}, "File", "readAsDataURL", [this.fileName]);
};
/**
* Read file and return data as a binary data.
*
* @param file {File} File object containing file properties
*/
FileReader.prototype.readAsBinaryString = function(file) {
// TODO - Can't return binary data to browser.
console.log('method "readAsBinaryString" is not supported at this time.');
};
/**
* Read file and return data as a binary data.
*
* @param file {File} File object containing file properties
*/
FileReader.prototype.readAsArrayBuffer = function(file) {
// TODO - Can't return binary data to browser.
console.log('This method is not supported at this time.');
};
module.exports = FileReader;