You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by br...@apache.org on 2013/03/07 16:03:30 UTC
js commit: Add readAsBinaryString and readAsArrayBuffer to FileReader
Updated Branches:
refs/heads/master 6df50c0ee -> 816f094ba
Add readAsBinaryString and readAsArrayBuffer to FileReader
Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/816f094b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/816f094b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/816f094b
Branch: refs/heads/master
Commit: 816f094ba5bd57da057b8dbd383f6f0be84eb8d2
Parents: 6df50c0
Author: Braden Shepherdson <br...@gmail.com>
Authored: Mon Mar 4 14:12:51 2013 -0500
Committer: Braden Shepherdson <br...@gmail.com>
Committed: Thu Mar 7 10:03:20 2013 -0500
----------------------------------------------------------------------
lib/common/plugin/FileReader.js | 129 ++++++++++++++++++++++++++++++++--
1 files changed, 123 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-js/blob/816f094b/lib/common/plugin/FileReader.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/FileReader.js b/lib/common/plugin/FileReader.js
index d48e714..3a698c0 100644
--- a/lib/common/plugin/FileReader.js
+++ b/lib/common/plugin/FileReader.js
@@ -283,9 +283,69 @@ FileReader.prototype.readAsBinaryString = function(file) {
if (initRead(this, file)) {
return this._realReader.readAsBinaryString(file);
}
- // TODO - Can't return binary data to browser.
- console.log('method "readAsBinaryString" is not supported at this time.');
- this.abort();
+
+ var me = this;
+ var execArgs = [this._fileName];
+
+ // Maybe add slice parameters.
+ if (file.end < file.size) {
+ execArgs.push(file.start, file.end);
+ } else if (file.start > 0) {
+ execArgs.push(file.start);
+ }
+
+ // 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;
+
+ // Convert the result from an ArrayBuffer to a binary string.
+ me._result = String.fromCharCode.apply(null, new Uint8Array(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", "readAsArrayBuffer", execArgs);
+ // Yes, readAsArrayBuffer. The conversion is done in Javascript, since we can
+ // only exchange binary data across the bridge using ArrayBuffers.
};
/**
@@ -297,9 +357,66 @@ FileReader.prototype.readAsArrayBuffer = function(file) {
if (initRead(this, file)) {
return this._realReader.readAsArrayBuffer(file);
}
- // TODO - Can't return binary data to browser.
- console.log('This method is not supported at this time.');
- this.abort();
+
+ var me = this;
+ var execArgs = [this._fileName];
+
+ // Maybe add slice parameters.
+ if (file.end < file.size) {
+ execArgs.push(file.start, file.end);
+ } else if (file.start > 0) {
+ execArgs.push(file.start);
+ }
+
+ // 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;
+
+ 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", "readAsArrayBuffer", execArgs);
};
module.exports = FileReader;