File Transfer example


The index.html file containing JavaScript that makes use of the File Transfer API functionality is listed below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html manifest="manifest.appcache">
  <head>
    <title>FileTransfer API usage example</title>
    <link rel="x-antenna-managed-webapp-descriptor" href="webapp-descriptor.xml" />
    <script type="text/javascript">
     
    function printText(str) {
      var d = document.getElementById('text-box');
      d.appendChild(document.createTextNode(str));
      d.appendChild(document.createElement('br'));
      d.scrollTop = d.scrollHeight;
    }
     
    function getFile(file) {
      var callback = {
        onSuccess: function (entry) {
          printText("onSuccess " + JSON.stringify(entry));
        },
        onProgress: function (progress) {
          printText("onProgress " + progress);
        },
        onCancel: function () {
          printText("onCancel");
        },
        onFailure: function (data) {
          printText("onFailure " + JSON.stringify(data));
      }};
      var options = undefined;
      requestFileSystem(window.PERSISTENT, 1 * 1024 * 1024 /*1MB*/, function (fs) {
        fs.root.getFile(file, {create: true}, function (entry) {
          lastTask = launchbox.FileTransfer.download("http://websitetips.com/articles/copy/lorem/"+file, entry.toURL(), options, callback);
        }, function (err) {
        printText("getFile error: " + JSON.stringify(err));
        });
      }, function (err) {
        printText("requestFileSystem error: " + JSON.stringify(err));
      });
    }
 
    function readFile(file){ //This function is not connected with File Transfer Api. For more information about methods used here look at the Filesystem API
      var errorHandler = function(err) {
        printText(err);
      }
      requestFileSystem(
        window.PERSISTENT,
        10*1024*1024 /*10MB*/,
        function(fs) {
          fs.root.getFile(file, {}, function(entry) {
            entry.file(function(fd) {
              var reader = new FileReader();
              reader.onload = function(event) {
                printText("Read: " + reader.result);
              };
              reader.onerror = function(event) {
                printText("Error occured.");
              };
              reader.readAsText(fd);
            }, errorHandler);
          }, errorHandler);
        },
        errorHandler
      );
    }
     
    function clearOutput(){
      var d = document.getElementById('text-box');    
      d.innerHTML = "";
    }
  </script>
  </head>
 
  <body>
    <header>
      <h3><span>FileTransfer API usage example</span>
      </h3>
    </header>
     
    <input type="button" onclick="getFile('ipsum.txt');" value="Download sample lorem ipsum txt file" /><br/>
    <input type="button" onclick="readFile('ipsum.txt');" value="Read a file"/><br/>
    <input type="button" onclick="getFile('error.asd');" value="Try to download non existing file" /><br/>
    <input type="button" onclick="clearOutput();" value="Clear output" />
    <div id="text-box"></div>
  </body>
</html>

The contents of the cache manifest file called manifest.appcache for this application are listed below:

1
2
3
4
5
6
7
CACHE MANIFEST
 
CACHE:
index.html
 
NETWORK:
*

The webapp-descriptor.xml file for this application is defined in the following way:

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<webapp-descriptor
    xmlns="http://www.antennasoftware.com/application-hosting/web-app-descriptor/2.0">
    <id>com.pega.sample.filetransfer</id>
    <version>1.0.0</version>
    <name>File Transfer API usage example</name>
</webapp-descriptor>

Related topics

Public API reference
Overview
Legal notice | Copyright © 2015 and Confidential to Pegasystems Inc. All rights reserved. | Feedback
Advanced...