Downloading multiple text files to the device and showing transfer progress

Downloading multiple text files to the device and showing transfer progress

I have been tasked with creating what on paper seems a simple mobile application. Because of the portability, we have chosen the Intel XDK as our development tool utilising HTML5/JQuery.

Basically, the mobile app will have three options:

 1. Download data - stock, operators, accounts etc. 
 2. Process transactions - record items allocated to the end user and by whom.
 3. Upload transactions - pass transaction details back to Back Office
    system.

I have made a start on the first of the options, Download data, where a number of text files need to be copied to the device, read in turn and imported into a database for use with the second option.
 
My code below is an attempt to show 3 buttons on the first 'page'. The app swaps to another page on pressing the download data button, which shows some blurb and Yes/No buttons. On pressing Yes, the app swaps to a further page where I would like to show the progress of each file being downloaded.
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4.   * Copyright © 2012-2015, Intel Corporation. All rights reserved.
  5.   * Please see the included LICENSE.md file for license terms and conditions.
  6.   -->
  7. <head>
  8.     <title>Gift of Kit</title>
  9.     <meta http-equiv="Content-type" content="text/html; charset=utf-8">

  10. <!-- This is a blank template, include any desired javascript frameworks, libraries, CSS here and start developing your app  -->

  11.     <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=no">

  12.     <style>
  13.         @-ms-viewport { width: 100vw ; min-zoom: 100% ; zoom: 100% ; }          @viewport { width: 100vw ; min-zoom: 100%; zoom: 100% ; }
  14.         @-ms-viewport { user-zoom: fixed ; min-zoom: 100% ; }                   @viewport { user-zoom: fixed ; min-zoom: 100% ; }
  15.     </style>

  16. <!-- Recommended location for your JavaScript libraries -->
  17.     <script src="cordova.js" id="xdkJScordova_"></script>
  18.     <link rel="stylesheet" href="themes/giftofkit.css" />
  19.     <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
  20.     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
  21.     <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  22.     <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

  23.     <script src="js/main.js"></script>
  24.     <script>
  25.         $(document).ready(function() {
  26.         var bodyHeight = $("body").height();
  27.         var vwptHeight = $(window).height();
  28.         if (vwptHeight > bodyHeight) {
  29.             $("div.pgFooter").css("position","absolute").css("bottom",0);
  30.         }
  31.         });      

  32.     </script>

  33.     <script id="xdkJSonDeviceReady_">
  34.         function onDeviceReady() {                                          // ignored by "Standard HTML5 web app"
  35.             if( navigator.splashscreen && navigator.splashscreen.hide ) {   // Cordova hide splashscreen detected, use it
  36.                 navigator.splashscreen.hide() ;
  37.             }
  38.         }
  39.         document.addEventListener("deviceready", onDeviceReady, false) ;    // ignored by "Standard HTML5 web app"
  40.     </script>
  41.     <link rel="stylesheet" href="css/main.css" type="text/css">

  42. </head>
  43. <body>
  44.     <div data-role="page" id="pgMenu" class="pgMenu">
  45.         <div data-role="header" id="pgMenuHeader" class="pgHeader">
  46.             <b>Gift of Kit</b>
  47.         </div>

  48.         <div data-role="main" class="pgContent">
  49.             <div data-role="controlgroup" data-type="vertical">
  50.                 <a href="#pgDownload" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Download Data</a>
  51.                 <a href="#pgProcess" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Process Transactions</a>
  52.                 <a href="#pgUpload" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Upload Transactions</a>
  53.             </div>
  54.         </div>

  55.         <div data-role="footer" id="pgMenuFooter" class="pgFooter">
  56.             <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">&copy; Medoc Computers</b>
  57.         </div>
  58.     </div> 
  59.     <div data-role="page" id="pgDownload">
  60.         <div data-role="header" id="pgDownloadHeader" class="pgHeader">
  61.             <b>Gift of Kit</b>
  62.         </div>

  63.         <div data-role="main" class="pgContent">
  64.             <p>You are about to download multiple files to this device and refresh the database.</p>
  65.             <p>Please confirm you wish to continue.</p>
  66.             <div data-role="controlgroup" data-type="horizontal">
  67.                 <a href="#pgImport" class="ui-btn ui-corner-all ui-shadow" data-transition="flip" id="btnYes">Yes</a>
  68.                 <a href="#" class="ui-btn ui-corner-all ui-shadow" data-rel="back">No</a>
  69.             </div>
  70.         </div>

  71.         <div data-role="footer" id="pgDownloadFooter" class="pgFooter">
  72.             <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">&copy; Medoc Computers</b>
  73.         </div>
  74.     </div> 
  75.     <div data-role="page" id="pgImport">
  76.         <div data-role="header" id="pgImportHeader" class="pgHeader">
  77.             <b>Gift of Kit</b>
  78.         </div>

  79.         <div data-role="main" class="pgContent">
  80.             <progress value="0" min="0" max="100" id="progress"></progress>
  81.             <div id="size"></div>
  82.             <div id="downloaded"></div>
  83.             
  84.             <a href="#pgMenu" class="ui-btn ui-corner-all ui-shadow" data-transition="flip">Go Back</a>
  85.         </div>

  86.         <div data-role="footer" id="pgImportFooter" class="pgFooter">
  87.             <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">&copy; Medoc Computers</b>
  88.         </div>
  89.     </div> 
  90.     <div data-role="page" id="pgProcess">
  91.         <div data-role="header" id="pgProcessHeader" class="pgHeader">
  92.             <b>Gift of Kit</b>
  93.         </div>

  94.         <div data-role="main" class="pgContent">
  95.             <a href="#" class="ui-btn ui-corner-all ui-shadow" data-rel="back">Go Back</a>
  96.         </div>

  97.         <div data-role="footer" id="pgProcessFooter" class="pgFooter">
  98.             <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">&copy; Medoc Computers</b>
  99.         </div>
  100.     </div> 
  101.     <div data-role="page" id="pgUpload">
  102.         <div data-role="header" id="pgUploadHeader" class="pgHeader">
  103.             <b>Gift of Kit</b>
  104.         </div>

  105.         <div data-role="main" class="pgContent">
  106.             <p id="temp"></p>
  107.             <a href="#" class="ui-btn ui-corner-all ui-shadow" data-rel="back">Go Back</a>
  108.         </div>

  109.         <div data-role="footer" id="pgUploadFooter" class="pgFooter">
  110.             <img src="images/blank.png" class="logo" alt="Medoc Computers" /><b class="pgFooter">&copy; Medoc Computers</b>
  111.         </div>
  112.     </div> 
  113.     <script>
  114.         $( "#pgImport" ).focus(function( event ) {
  115.             var file1State = downloadFile("S\zcaug13");
  116.             var file2State = downloadFile("S\safc_2feb");
  117.             var file3State = downloadFile("S\sales_query.sql");
  118.        
  119.             if ( file1State == 0 || file2State == 0 || file3State == 0 ) {
  120.                 alert("Downloading Cancelled");
  121.             } else if ( file1State == 1 || file2State == 1 || file3State == 1 ) {
  122.                 alert("Downloading Failed");
  123.             } else {
  124.                 alert("Downloading Successful");
  125.             }
  126.         
  127.         });
  128.         
  129.         function downloadFile(fn) {
  130.             var fileName = fn;
  131.             var xhr = new XMLHttpRequest();
  132.             var stat;
  133.         
  134.             xhr.addEventListener("progress", updateProgress, false);
  135.             xhr.addEventListener("load", transferComplete, false);
  136.             xhr.addEventListener("error", transferFailed, false);
  137.             xhr.addEventListener("abort", transferCanceled, false);

  138.             xhr.open("GET", fileName);
  139.         
  140.             function updateProgress(oEvent) 
  141.             {
  142.                 if (oEvent.lengthComputable) 
  143.                 {
  144.                     var percentComplete = (oEvent.loaded / oEvent.total) * 100;            
  145.                     document.getElementById("progress").setAttribute("value", percentComplete);//just a random value
  146.                     document.getElementById("size").innerHTML = oEvent.total;
  147.                     document.getElementById("downloaded").innerHTML = oEvent.loaded; 
  148.                 }    
  149.                 else 
  150.                 {
  151.                     document.getElementById("progress").setAttribute("value", 30);//just a random value
  152.                     document.getElementById("size").innerHTML = "unknown";
  153.                     document.getElementById("downloaded").innerHTML = "unknown"; 
  154.                 }
  155.             }
  156.         
  157.             function transferComplete(oEvent)
  158.             {
  159.                 return 2;
  160.             }
  161.         
  162.             function transferFailed(oEvent)
  163.             {
  164.                 return 1;
  165.             }
  166.         
  167.             function transferCanceled(oEvent)
  168.             {
  169.                 return 0;
  170.             }
  171.         
  172.             xhr.send();
  173.         }
  174.     </script>
  175. </body>
  176. </html>

However my knowledge of HTML5/JQuery is not good and I have already hit my first hurdle.

The file download is happening before the progress page is displayed.

I have a couple of questions:

 1. How do I make the file download happen after the progress page as been displayed?
 2. How do I make it show progress for each file in turn?

Any help greatly appreciated.