cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject js commit: [CB-2406] Add FileWriter.write(blob|arrayBuffer) support
Date Wed, 12 Jun 2013 17:35:35 GMT
Updated Branches:
  refs/heads/master 62c57866b -> 9c6c6374b


[CB-2406] Add FileWriter.write(blob|arrayBuffer) support


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/9c6c6374
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/9c6c6374
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/9c6c6374

Branch: refs/heads/master
Commit: 9c6c6374b7f771fb95c2eabd123e070c5011179e
Parents: 62c5786
Author: Ian Clelland <iclelland@chromium.org>
Authored: Tue Jun 11 14:10:23 2013 -0400
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Wed Jun 12 11:09:06 2013 -0400

----------------------------------------------------------------------
 lib/common/plugin/FileWriter.js | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/9c6c6374/lib/common/plugin/FileWriter.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/FileWriter.js b/lib/common/plugin/FileWriter.js
index 9dbbd81..e9930ab 100644
--- a/lib/common/plugin/FileWriter.js
+++ b/lib/common/plugin/FileWriter.js
@@ -93,9 +93,25 @@ FileWriter.prototype.abort = function() {
 /**
  * Writes data to the file
  *
- * @param text to be written
+ * @param data text or blob to be written
  */
-FileWriter.prototype.write = function(text) {
+FileWriter.prototype.write = function(data) {
+
+    // Check to see if the incoming data is a blob
+    if (data instanceof Blob) {
+        var that=this;
+        var fileReader = new FileReader();
+        fileReader.onload = function() {
+            // Call this method again, with the arraybuffer as argument
+            FileWriter.prototype.write.call(that, this.result);
+        };
+        fileReader.readAsArrayBuffer(data);
+        return;
+    }
+
+    // Mark data type for safer transport over the binary bridge
+    var isBinary = (data instanceof ArrayBuffer);
+
     // Throw an exception if we are already writing a file
     if (this.readyState === FileWriter.WRITING) {
         throw new FileError(FileError.INVALID_STATE_ERR);
@@ -161,7 +177,7 @@ FileWriter.prototype.write = function(text) {
             if (typeof me.onwriteend === "function") {
                 me.onwriteend(new ProgressEvent("writeend", {"target":me}));
             }
-        }, "File", "write", [this.fileName, text, this.position]);
+        }, "File", "write", [this.fileName, data, this.position, isBinary]);
 };
 
 /**


Mime
View raw message