From Web Computing Documentation
Jump to: navigation, search

class Downloader

  • web2grid
    • web2grid.core

Class overview

A Downloader can download multiple files specified by their URL using HTTP requests. It can return an AsyncResult corresponding to the download process. The AsyncResult can be polled for the actual state of the downloads or you can subscribe to its events corresponding to change in progress and completion.

Public functions

new ()

Constructor, has no parameters.

add (url : String)

url The url of the file to download.

Takes a valid url string that points to the file you intend to download. Once you've instantiated a Downloader object use this function to add all files before starting the download.

downloadAll () : AsyncResult<Hash<FileStream>>

Starts the download process. Returns an AsyncResult<Hash<FileStream>>. You can subscribe to the return value's onComplete event or check its isCompleted property. Once the downloads are ready, use the getResult() function of the AsyncResult to retrieve a Hash<FileStream>. The keys of the hash will be urls used with the add() function and the corresponding FileStream will contain the downloaded file.


In the following example we use the Downloader class to retrieve two files from two different sources. Once the download has been completed we call a function with the downloaded files as String parameters.

import package;
var myDownloader = new Downloader();
var downloaderAsyncResult = myDownloader.downloadAll();
downloaderAsyncResult.onComplete.subscribe(function(streams : Hash<FileStream>)
    var res1 = streams.get('');
    var res2 = streams.get('');
    myFunction(res1.getContent(), res2.getContent());

We used the object given to the callback function here, but we could also have used the getResult() method of downloaderAsyncResult.

Personal tools