Downloader

From Web Computing Documentation
Revision as of 13:35, 12 August 2011 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

class Downloader

  • web2grid
    • web2grid.core
      • web2grid.core.net
        • web2grid.core.net.Downloader


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.

Example

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 web2grid.core.net.Downloader.hx;
 
var myDownloader = new Downloader();
myDownloader.add("http://server1.com/resource1.txt");
myDownloader.add("http://server2.com/resource2.txt");
 
var downloaderAsyncResult = myDownloader.downloadAll();
downloaderAsyncResult.onComplete.subscribe(function(streams : Hash<FileStream>)
{
    var res1 = streams.get('http://server1.com/resource1.txt');
    var res2 = streams.get('http://server2.com/resource2.txt');
    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
Modules