You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Mikejo5000 <gi...@git.apache.org> on 2016/04/01 23:05:52 UTC

[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

GitHub user Mikejo5000 opened a pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136

    adding sample section to readme

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/Mikejo5000/cordova-plugin-file-transfer master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #136
    
----
commit 013a3b60a20e03e69b7403c355033397453df870
Author: Mikejo5001 <m....@outlook.com>
Date:   2016-04-01T21:04:31Z

    adding sample section to readme

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59297145
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    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 function is the success handler, 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.
    +
    +```js
    +function download(fileEntry, uri) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(entry);
    +        },
    +        function (error) {
    +            console.log("download error source " + error.source);
    --- End diff --
    
    Consider adding ":" between the string and the value in these examples.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by riknoll <gi...@git.apache.org>.
Github user riknoll commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r58279204
  
    --- Diff: README.md ---
    @@ -315,3 +315,237 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     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:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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 using `resolveLocalFileSystemURL`, passing in a Cordova file URL like `cordova.file.cacheDirectory`. Use the `getFile` method of DirectoryEntry to create the target file.
    +
    +```
    +window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
    --- End diff --
    
    No need to use `requestFileSystem` and `resolveLocalFileSystemURL` together to access the cache


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r58437220
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     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:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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';
    +    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 function is the success handler, 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) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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 (e) {
    +
    +            console.log("Successful file read: " + this.result);
    +            displayFileData(fileEntry.fullPath + ": " + this.result);
    +
    +            var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
    +            displayImageData(blob);
    +        };
    +
    +        reader.readAsArrayBuffer(file);
    +
    +    }, onErrorReadFile);
    +}
    +```
    +
    +Once you read the file successfully, create a DOM URL string using `createObjectURL`, and then display the image.
    +
    +```
    +function displayImageData(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;
    +}
    +```
    +
    +## 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 (e) {
    +            console.log("Successful file read...");
    +            upload(fileEntry);
    +        };
    +
    +        fileWriter.onerror = function (e) {
    +            console.log("Failed file read: " + e.toString());
    --- End diff --
    
    read => write?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59295798
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    --- End diff --
    
    It might be a good idea to make these hyperlinks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r58437055
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     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:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```
    --- End diff --
    
    Please add language hints here: ````js`. This helps with syntax highlighting.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by daserge <gi...@git.apache.org>.
Github user daserge commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59332428
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    fs.root.getFile('downloaded-image.png', { create: true, exclusive: false }, function (fileEntry) {
    +        download(fileEntry, url, true);
    --- End diff --
    
    It is stated below that `The 3rd argument to the function is the success handler` and `true` is passed here - could you please clarify this?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#issuecomment-208605754
  
    Minor comments. Otherwise LGTM.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r58437200
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     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:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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';
    +    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 function is the success handler, 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) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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 (e) {
    +
    +            console.log("Successful file read: " + this.result);
    +            displayFileData(fileEntry.fullPath + ": " + this.result);
    +
    +            var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
    +            displayImageData(blob);
    +        };
    +
    +        reader.readAsArrayBuffer(file);
    +
    +    }, onErrorReadFile);
    +}
    +```
    +
    +Once you read the file successfully, create a DOM URL string using `createObjectURL`, and then display the image.
    +
    +```
    +function displayImageData(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;
    +}
    +```
    +
    +## 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 (e) {
    +            console.log("Successful file read...");
    --- End diff --
    
    read => write ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59297187
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    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 function is the success handler, 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.
    +
    +```js
    +function download(fileEntry, uri) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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.
    +
    +```js
    +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, create a DOM URL string using `createObjectURL`, and then display the image.
    +
    +```js
    +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;
    +}
    +```
    +
    +## 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.
    +
    +```js
    +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.
    +
    +```js
    +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.
    +
    +```js
    +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("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.
    +
    +```js
    +function download(fileEntry, uri) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readFile(entry);
    +        },
    +        function (error) {
    +            console.log("download error source " + error.source);
    --- End diff --
    
    Here as well.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59297886
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    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 function is the success handler, 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.
    +
    +```js
    +function download(fileEntry, uri) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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.
    +
    +```js
    +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, create a DOM URL string using `createObjectURL`, and then display the image.
    +
    +```js
    +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;
    +}
    +```
    +
    +## 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.
    +
    +```js
    +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.
    +
    +```js
    +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.
    +
    +```js
    +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("Code = " + r.responseCode);
    --- End diff --
    
    Should we include "Upload successful" ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by riknoll <gi...@git.apache.org>.
Github user riknoll commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r58279713
  
    --- Diff: README.md ---
    @@ -315,3 +315,237 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     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:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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 using `resolveLocalFileSystemURL`, passing in a Cordova file URL like `cordova.file.cacheDirectory`. 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);
    +    // Return a DirectoryEntry using Cordova file URLs.
    +    window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function (dirEntry) {
    +
    +        // 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';
    +        var fileName = 'downloaded-image.png';
    +
    +        dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
    +            download(fileEntry, url);
    +
    +        }, onErrorCreateFile);
    +
    +    }, onErrorResolveUrl);
    +
    +}, onErrorLoadFs);
    +```
    +
    +>*Note* For persistent storage, use cordova.file.dataDirectory and 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 function is the success handler, 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) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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.
    --- End diff --
    
    The second readAsArrayBuffer isn't in an inline code block


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#issuecomment-208437365
  
    @daserge @rakatyal to help review


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59296514
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    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 function is the success handler, 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.
    +
    +```js
    +function download(fileEntry, uri) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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.
    +
    +```js
    +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);
    --- End diff --
    
    Do we have this function defined?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by rakatyal <gi...@git.apache.org>.
Github user rakatyal commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59295678
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    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.
    --- End diff --
    
    Consider adding a note explaining the 'exclusive' and 'create' options for the getFile function.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-plugin-file-transfer pull request: adding sample section t...

Posted by daserge <gi...@git.apache.org>.
Github user daserge commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-file-transfer/pull/136#discussion_r59334240
  
    --- Diff: README.md ---
    @@ -315,3 +315,231 @@ If you are upgrading to a new (1.0.0 or newer) version of File, and you have pre
         cdvfile://localhost/persistent/path/to/file
     
     which can be used in place of the absolute file path in both `download()` and `upload()` methods.
    +
    +## Sample: Download and Upload Files <a name="sample"></a>
    +
    +Use the File-Transfer plugin to upload and download files. In these examples, we demonstrate several tasks like:
    +
    +* Downloading a binary file to the application cache
    +* Uploading a file created in your application's root
    +* Downloading the uploaded file
    +
    +## 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.
    +
    +```js
    +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';
    +    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 function is the success handler, 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.
    +
    +```js
    +function download(fileEntry, uri) {
    +
    +    var fileTransfer = new FileTransfer();
    +    var fileURL = fileEntry.toURL();
    +
    +    fileTransfer.download(
    +        uri,
    +        fileURL,
    +        function (entry) {
    +            console.log("download complete: " + entry.toURL());
    +            readBinaryFile(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=="
    +            //}
    +        }
    +    );
    +}
    +```
    +
    +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.
    +
    +```js
    +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, create a DOM URL string using `createObjectURL`, and then display the image.
    +
    +```js
    +function displayImage(blob) {
    +
    +    // Note: Use window.URL.revokeObjectURL when finished with image.
    +    var objURL = window.URL.createObjectURL(blob);
    --- End diff --
    
    Does it make sense to also add a simpler example using `img.src = entry.toURL();` like we have in [the test suite](https://github.com/apache/cordova-plugin-file-transfer/blob/master/tests/tests.js#L1583)?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org