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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]