[Adobe AIR] – Resume Download

August 26, 2012 at 2:45 PM (Actionscript) (, , )

In one of the project I’m working on, we have a need to support resume download feature, as the app depends on very large asset files(larger than 200MB) which are downloaded separately …
Looking for a solution to this, hit upon Range header that can be defined using ‘URLRequestHeader’ class…

var custom_header:URLRequestHeader = new URLRequestHeader("range","bytes="+fromBytePos+"-"+toBytePos);

Using ‘urlStream’ class for the downloading the assets, we could simply define a custom header to receive only particular range of bytes, which will help us when we want to resume a paused/broken download

var custom_header:URLRequestHeader = new URLRequestHeader("range","bytes="+fromBytePos+"-"+toBytePos);
var request:URLRequest = new URLRequest(asset_url);
request.requestHeaders.push(custom_header);
var asset_downstream:URLStream = new URLStream();
asset_downstream.load(request);

Once a download starts keep track of bytes loaded so far,totalBytes of the file,file path where its is saved, and have a flag say ‘download_complete’ which contains whether the download is complete or not in a log file or simply in sharedObject.
If you simply want to resume a paused download, use the above code where fromBytePos will be bytes loaded and toBytePos will totalBytes…
If the app was closed or terminated for some reasons, when the app starts first check if there is any pending download using the flag ‘download_complete’, if it is ‘false’ start the download process based on saved parameters…

will post complete code of a simple download manager class based on this, soon…

Permalink Leave a Comment

Next page »

%d bloggers like this: