I`m just developing an AIR project were the client needs to be able to synchronize big loads of data. So I thought it would be cool if the user can pause and resume downloads.
I first thought that this would not be possible but then i looked at the Adobe Media Player and he does it as well (Look into the Download Manager). The trick seems to be easy.
After a quick search through the Http specification i found out that you can write a custom header to use with the URLStream class. With Http specification 1.1 you can define a range of bytes you want to download so you don`t have to start from the beginning.
Here is how a sample Request looks like:
GET /files/richflv/help/ExportOptions.flv HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; en) AppleWebKit/420+ (KHTML, like Gecko) AdobeAIR/1.0
In Actionscript we can write a custom header like this:
var header1:URLRequestHeader = new URLRequestHeader(“range”,”bytes=”+currentBytesPosition+”-“+toRange);
var re:URLRequest = new URLRequest(downLoadURL);
currentBytesPosition = toRange;
This should work with most servers/files. To make sure it works you can first make a request and analyse the response header. It should include something like this:
I think you get the idea! This is pretty powerfull especially if you want to synchronize big chunks of binary data and the client closes the client in between.
On the next startup you can simply resume the download at the position were the client closed your AIR app. There are so many hidden goodies in AIR/AS3.0!