Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AD5472009C5 for ; Mon, 16 May 2016 23:17:56 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id ABE21160A19; Mon, 16 May 2016 21:17:56 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D6F1B160131 for ; Mon, 16 May 2016 23:17:54 +0200 (CEST) Received: (qmail 22310 invoked by uid 500); 16 May 2016 21:17:54 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 22301 invoked by uid 99); 16 May 2016 21:17:54 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 May 2016 21:17:54 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 93A9FC0A28 for ; Mon, 16 May 2016 21:17:53 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.426 X-Spam-Level: X-Spam-Status: No, score=-0.426 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id EDjYFsdVU1n2 for ; Mon, 16 May 2016 21:17:49 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id B89C25FB73 for ; Mon, 16 May 2016 21:17:48 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 673B6E1003 for ; Mon, 16 May 2016 21:17:47 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 339FD3A17ED for ; Mon, 16 May 2016 21:17:47 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1744142 [5/9] - in /cordova/site/public: ./ docs/en/6.x/ docs/en/6.x/config_ref/ docs/en/6.x/cordova/events/ docs/en/6.x/cordova/storage/ docs/en/6.x/guide/platforms/win8/ docs/en/6.x/plugin_ref/ docs/en/6.x/reference/cordova-cli/ docs/en/... Date: Mon, 16 May 2016 21:17:45 -0000 To: commits@cordova.apache.org From: rakatyal@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160516211747.339FD3A17ED@svn01-us-west.apache.org> archived-at: Mon, 16 May 2016 21:17:56 -0000 Modified: cordova/site/public/docs/en/6.x/reference/cordova-plugin-file-transfer/index.html URL: http://svn.apache.org/viewvc/cordova/site/public/docs/en/6.x/reference/cordova-plugin-file-transfer/index.html?rev=1744142&r1=1744141&r2=1744142&view=diff ============================================================================== --- cordova/site/public/docs/en/6.x/reference/cordova-plugin-file-transfer/index.html (original) +++ cordova/site/public/docs/en/6.x/reference/cordova-plugin-file-transfer/index.html Mon May 16 21:17:45 2016 @@ -2687,15 +2687,15 @@

This plugin allows you to upload and download files.

This plugin defines global FileTransfer, FileUploadOptions constructors. Although in the global scope, they are not available until after the deviceready event.

-
document.addEventListener("deviceready", onDeviceReady, false);
-function onDeviceReady() {
-    console.log(FileTransfer);
-}
+
document.addEventListener("deviceready", onDeviceReady, false);
+function onDeviceReady() {
+    console.log(FileTransfer);
+}
 

Report issues with this plugin on the Apache Cordova issue tracker

Installation

-
cordova plugin add cordova-plugin-file-transfer
+
cordova plugin add cordova-plugin-file-transfer
 

Supported Platforms

@@ -2757,68 +2757,68 @@ multi-part POST or PUT request, and to d

Example

-
// !! Assumes variable fileURL contains a valid URL to a text file on the device,
-//    for example, cdvfile://localhost/persistent/path/to/file.txt
+
// !! Assumes variable fileURL contains a valid URL to a text file on the device,
+//    for example, cdvfile://localhost/persistent/path/to/file.txt
 
-var win = function (r) {
-    console.log("Code = " + r.responseCode);
-    console.log("Response = " + r.response);
-    console.log("Sent = " + r.bytesSent);
-}
-
-var fail = function (error) {
-    alert("An error has occurred: Code = " + error.code);
-    console.log("upload error source " + error.source);
-    console.log("upload error target " + error.target);
-}
-
-var options = new FileUploadOptions();
-options.fileKey = "file";
-options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
-options.mimeType = "text/plain";
-
-var params = {};
-params.value1 = "test";
-params.value2 = "param";
+var win = function (r) {
+    console.log("Code = " + r.responseCode);
+    console.log("Response = " + r.response);
+    console.log("Sent = " + r.bytesSent);
+}
+
+var fail = function (error) {
+    alert("An error has occurred: Code = " + error.code);
+    console.log("upload error source " + error.source);
+    console.log("upload error target " + error.target);
+}
+
+var options = new FileUploadOptions();
+options.fileKey = "file";
+options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
+options.mimeType = "text/plain";
+
+var params = {};
+params.value1 = "test";
+params.value2 = "param";
 
-options.params = params;
+options.params = params;
 
-var ft = new FileTransfer();
-ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
+var ft = new FileTransfer();
+ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
 

Example with Upload Headers and Progress Events (Android and iOS only)

-
function win(r) {
-    console.log("Code = " + r.responseCode);
-    console.log("Response = " + r.response);
-    console.log("Sent = " + r.bytesSent);
-}
-
-function fail(error) {
-    alert("An error has occurred: Code = " + error.code);
-    console.log("upload error source " + error.source);
-    console.log("upload error target " + error.target);
-}
-
-var uri = encodeURI("http://some.server.com/upload.php");
-
-var options = new FileUploadOptions();
-options.fileKey="file";
-options.fileName=fileURL.substr(fileURL.lastIndexOf('/')+1);
-options.mimeType="text/plain";
-
-var headers={'headerParam':'headerValue'};
-
-options.headers = headers;
-
-var ft = new FileTransfer();
-ft.onprogress = function(progressEvent) {
-    if (progressEvent.lengthComputable) {
-      loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
-    } else {
-      loadingStatus.increment();
-    }
-};
-ft.upload(fileURL, uri, win, fail, options);
+
function win(r) {
+    console.log("Code = " + r.responseCode);
+    console.log("Response = " + r.response);
+    console.log("Sent = " + r.bytesSent);
+}
+
+function fail(error) {
+    alert("An error has occurred: Code = " + error.code);
+    console.log("upload error source " + error.source);
+    console.log("upload error target " + error.target);
+}
+
+var uri = encodeURI("http://some.server.com/upload.php");
+
+var options = new FileUploadOptions();
+options.fileKey="file";
+options.fileName=fileURL.substr(fileURL.lastIndexOf('/')+1);
+options.mimeType="text/plain";
+
+var headers={'headerParam':'headerValue'};
+
+options.headers = headers;
+
+var ft = new FileTransfer();
+ft.onprogress = function(progressEvent) {
+    if (progressEvent.lengthComputable) {
+        loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
+    } else {
+        loadingStatus.increment();
+    }
+};
+ft.upload(fileURL, uri, win, fail, options);
 

FileUploadResult

@@ -2870,30 +2870,30 @@ ft.upload(fileURL, uri, win, fail, optio

Example

-
// !! Assumes variable fileURL contains a valid URL to a path on the device,
-//    for example, cdvfile://localhost/persistent/path/to/downloads/
+
// !! Assumes variable fileURL contains a valid URL to a path on the device,
+//    for example, cdvfile://localhost/persistent/path/to/downloads/
 
-var fileTransfer = new FileTransfer();
-var uri = encodeURI("http://some.server.com/download.php");
+var fileTransfer = new FileTransfer();
+var uri = encodeURI("http://some.server.com/download.php");
 
-fileTransfer.download(
-    uri,
-    fileURL,
-    function(entry) {
-        console.log("download complete: " + entry.toURL());
-    },
-    function(error) {
-        console.log("download error source " + error.source);
-        console.log("download error target " + error.target);
-        console.log("upload error code" + error.code);
-    },
-    false,
-    {
-        headers: {
-            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
-        }
-    }
-);
+fileTransfer.download(
+    uri,
+    fileURL,
+    function(entry) {
+        console.log("download complete: " + entry.toURL());
+    },
+    function(error) {
+        console.log("download error source " + error.source);
+        console.log("download error target " + error.target);
+        console.log("upload error code" + error.code);
+    },
+    false,
+    {
+        headers: {
+            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
+        }
+    }
+);
 

WP8 Quirks

@@ -2912,28 +2912,28 @@ fileTransfer.download(

Aborts an in-progress transfer. The onerror callback is passed a FileTransferError object which has an error code of FileTransferError.ABORT_ERR.

Example

-
// !! Assumes variable fileURL contains a valid URL to a text file on the device,
-//    for example, cdvfile://localhost/persistent/path/to/file.txt
+
// !! Assumes variable fileURL contains a valid URL to a text file on the device,
+//    for example, cdvfile://localhost/persistent/path/to/file.txt
 
-var win = function(r) {
-    console.log("Should not be called.");
-}
-
-var fail = function(error) {
-    // error.code == FileTransferError.ABORT_ERR
-    alert("An error has occurred: Code = " + error.code);
-    console.log("upload error source " + error.source);
-    console.log("upload error target " + error.target);
-}
-
-var options = new FileUploadOptions();
-options.fileKey="file";
-options.fileName="myphoto.jpg";
-options.mimeType="image/jpeg";
-
-var ft = new FileTransfer();
-ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
-ft.abort();
+var win = function(r) {
+    console.log("Should not be called.");
+}
+
+var fail = function(error) {
+    // error.code == FileTransferError.ABORT_ERR
+    alert("An error has occurred: Code = " + error.code);
+    console.log("upload error source " + error.source);
+    console.log("upload error target " + error.target);
+}
+
+var options = new FileUploadOptions();
+options.fileKey="file";
+options.fileName="myphoto.jpg";
+options.mimeType="image/jpeg";
+
+var ft = new FileTransfer();
+ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
+ft.abort();
 

FileTransferError

@@ -2983,6 +2983,229 @@ ft.abort();

which can be used in place of the absolute file path in both download() and upload() methods.

+

Sample: Download and Upload Files

+ +

Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:

+ + + +

Download a Binary File to the application cache

+ +

Use the File plugin with the File-Transfer plugin to provide a target for the files that you download (the target must be a FileEntry object). Before you download the file, create a DirectoryEntry object by using resolveLocalFileSystemURL and calling fs.root in the success callback. Use the getFile method of DirectoryEntry to create the target file.

+
window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
+
+    console.log('file system open: ' + fs.name);
+
+    // Make sure you add the domain name to the Content-Security-Policy <meta> element.
+    var url = 'http://cordova.apache.org/static/img/cordova_bot.png';
+    // Parameters passed to getFile create a new file or return the file if it already exists.
+    fs.root.getFile('downloaded-image.png', { create: true, exclusive: false }, function (fileEntry) {
+        download(fileEntry, url, true);
+
+    }, onErrorCreateFile);
+
+}, onErrorLoadFs);
+
+
+

Note For persistent storage, pass LocalFileSystem.PERSISTENT to requestFileSystem.

+
+ +

When you have the FileEntry object, download the file using the download method of the FileTransfer object. The 3rd argument to the download function of FileTransfer is the success callback, which you can use to call the app's readBinaryFile function. In this code example, the entry variable is a new FileEntry object that receives the result of the download operation.

+
function download(fileEntry, uri, readBinaryData) {
+
+    var fileTransfer = new FileTransfer();
+    var fileURL = fileEntry.toURL();
+
+    fileTransfer.download(
+        uri,
+        fileURL,
+        function (entry) {
+            console.log("Successful download...");
+            console.log("download complete: " + entry.toURL());
+            if (readBinaryData) {
+              // Read the file...
+              readBinaryFile(entry);
+            }
+            else {
+              // Or just display it.
+              displayImageByFileURL(entry);
+            }
+        },
+        function (error) {
+            console.log("download error source " + error.source);
+            console.log("download error target " + error.target);
+            console.log("upload error code" + error.code);
+        },
+        null, // or, pass false
+        {
+            //headers: {
+            //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
+            //}
+        }
+    );
+}
+
+

If you just need to display the image, take the FileEntry to call its toURL() function.

+
function displayImageByFileURL(fileEntry) {
+    var elem = document.getElementById('imageFile');
+    elem.src = fileEntry.toURL();
+}
+
+

Depending on your app requirements, you may want to read the file. To support operations with binary files, FileReader supports two methods, readAsBinaryString and readAsArrayBuffer. In this example, use readAsArrayBuffer and pass the FileEntry object to the method. Once you read the file successfully, construct a Blob object using the result of the read.

+
function readBinaryFile(fileEntry) {
+    fileEntry.file(function (file) {
+        var reader = new FileReader();
+
+        reader.onloadend = function() {
+
+            console.log("Successful file read: " + this.result);
+            // displayFileData(fileEntry.fullPath + ": " + this.result);
+
+            var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
+            displayImage(blob);
+        };
+
+        reader.readAsArrayBuffer(file);
+
+    }, onErrorReadFile);
+}
+
+

Once you read the file successfully, you can create a DOM URL string using createObjectURL, and then display the image.

+
function displayImage(blob) {
+
+    // Note: Use window.URL.revokeObjectURL when finished with image.
+    var objURL = window.URL.createObjectURL(blob);
+
+    // Displays image if result is a valid DOM string for an image.
+    var elem = document.getElementById('imageFile');
+    elem.src = objURL;
+}
+
+

As you saw previously, you can call FileEntry.toURL() instead to just display the downloaded image (skip the file read).

+ +

Upload a File +

When you upload a File using the File-Transfer plugin, use the File plugin to provide files for upload (again, they must be FileEntry objects). Before you can upload anything, create a file for upload using the getFile method of DirectoryEntry. In this example, create the file in the application's cache (fs.root). Then call the app's writeFile function so you have some content to upload.

+
function onUploadFile() {
+    window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
+
+        console.log('file system open: ' + fs.name);
+        var fileName = "uploadSource.txt";
+        var dirEntry = fs.root;
+        dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
+
+            // Write something to the file before uploading it.
+            writeFile(fileEntry);
+
+        }, onErrorCreateFile);
+
+    }, onErrorLoadFs);
+}
+
+

In this example, create some simple content, and then call the app's upload function.

+
function writeFile(fileEntry, dataObj) {
+    // Create a FileWriter object for our FileEntry (log.txt).
+    fileEntry.createWriter(function (fileWriter) {
+
+        fileWriter.onwriteend = function () {
+            console.log("Successful file write...");
+            upload(fileEntry);
+        };
+
+        fileWriter.onerror = function (e) {
+            console.log("Failed file write: " + e.toString());
+        };
+
+        if (!dataObj) {
+          dataObj = new Blob(['file data to upload'], { type: 'text/plain' });
+        }
+
+        fileWriter.write(dataObj);
+    });
+}
+
+

Forward the FileEntry object to the upload function. To perform the actual upload, use the upload function of the FileTransfer object.

+
function upload(fileEntry) {
+    // !! Assumes variable fileURL contains a valid URL to a text file on the device,
+    var fileURL = fileEntry.toURL();
+
+    var success = function (r) {
+        console.log("Successful upload...");
+        console.log("Code = " + r.responseCode);
+        displayFileData(fileEntry.fullPath + " (content uploaded to server)");
+    }
+
+    var fail = function (error) {
+        alert("An error has occurred: Code = " + error.code);
+    }
+
+    var options = new FileUploadOptions();
+    options.fileKey = "file";
+    options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
+    options.mimeType = "text/plain";
+
+    var params = {};
+    params.value1 = "test";
+    params.value2 = "param";
+
+    options.params = params;
+
+    var ft = new FileTransfer();
+    // SERVER must be a URL that can handle the request, like
+    // http://some.server.com/upload.php
+    ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
+};
+
+

Download the uploaded file

+ +

To download the image you just uploaded, you will need a valid URL that can handle the request, for example, http://some.server.com/download.php. Again, the success handler for the FileTransfer.download method receives a FileEntry object. The main difference here from previous examples is that we call FileReader.readAsText to read the result of the download operation, because we uploaded a file with text content.

+
function download(fileEntry, uri) {
+
+    var fileTransfer = new FileTransfer();
+    var fileURL = fileEntry.toURL();
+
+    fileTransfer.download(
+        uri,
+        fileURL,
+        function (entry) {
+            console.log("Successful download...");
+            console.log("download complete: " + entry.toURL());
+            readFile(entry);
+        },
+        function (error) {
+            console.log("download error source " + error.source);
+            console.log("download error target " + error.target);
+            console.log("upload error code" + error.code);
+        },
+        null, // or, pass false
+        {
+            //headers: {
+            //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
+            //}
+        }
+    );
+}
+
+

In the readFile function, call the readAsText method of the FileReader object.

+
function readFile(fileEntry) {
+    fileEntry.file(function (file) {
+        var reader = new FileReader();
+
+        reader.onloadend = function () {
+
+            console.log("Successful file read: " + this.result);
+            // displayFileData(fileEntry.fullPath + ": " + this.result);
+
+        };
+
+        reader.readAsText(file);
+
+    }, onErrorReadFile);
+}
+
--------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org For additional commands, e-mail: commits-help@cordova.apache.org