Samsung Electronics logo

Managing Downloads



To provide the user access to Internet resources, you must learn how to manage download operations:

  1. Create an instance of the DownloadRequest interface that defines the URL of the file to be downloaded:

    var downloadRequest = new webapis.DownloadRequest("http://www.samsung.com/index.html", "downloads");
    

    The final parameter (downloads) defines the folder where the downloaded content is stored. The parameter uses a relative folder location defined in the Filesystem API. The folder is not an absolute folder location, but instead uses a virtual root location (downloads is the default download location in the virtual root).

  2. Define the event handlers for different download process notifications using the DownloadCallback listener interface:
    var listener = 
    {
       /* When the download progresses (interval is platform-dependent) */
       onprogress: function(id, receivedSize, totalSize) 
       {
          console.log('Received with id: ' + id + ', ' + receivedSize + '/' + totalSize);
       },
    
       /* When the user pauses the download */
       onpaused: function(id) 
       {
          console.log('Paused with id: ' + id);
       },
    
       /* When the user cancels the download */
       oncanceled: function(id) 
       {
          console.log('Canceled with id: ' + id);
       },
    
       /* When the download is completed */
       oncompleted: function(id, fullPath) 
       {
          console.log('Completed with id: ' + id + ', full path: ' + fullPath);
       },
    
       /* When the download fails */
       onfailed: function(id, error) 
       {
          console.log('Failed with id: ' + id + ', error name: ' + error.name);
       }
    };
  3. To start the download of the HTML file from the Internet, use the start() method of the DownloadManager interface:

    downloadId = webapis.download.start(downloadRequest, listener);
    

    The start() method returns a unique identifier for the download operation.

  4. During the download:
    1. To pause the download, use the pause() method with the download ID:

      webapis.download.pause(downloadId);
      
    2. To resume the download, use the resume() method with the download ID:

      webapis.download.resume(downloadId);
      
    3. To cancel the download, use the cancel() method with the download ID:

      webapis.download.cancel(downloadId);