Skip to content

Instantly share code, notes, and snippets.

@peterblazejewicz
Created June 8, 2011 10:36
Show Gist options
  • Save peterblazejewicz/1014181 to your computer and use it in GitHub Desktop.
Save peterblazejewicz/1014181 to your computer and use it in GitHub Desktop.
background downloading using {mdm} script and showing loaded image (local) into spark component
package
{
import mdm.HTTP;
import mx.controls.ProgressBar;
import spark.components.Application;
import spark.components.Image;
public class MyApplication extends Application
{
public var progressBar:ProgressBar;
public var image:Image;
// this is set of high-res images hosted on nasa.gov
public static var images:Array = new Array(
"http://www.nasa.gov/images/content/557331main_iss027e036710_full.jpg",
"http://www.nasa.gov/images/content/556696main_5799770505_894b0ee923_o_full.jpg",
"http://www.nasa.gov/images/content/555240main_201106010004HQ_full.jpg",
"http://www.nasa.gov/images/content/555115main_1963_full.jpg",
"http://www.nasa.gov/images/content/554474main_iss028e006193-full_full.jpeg",
"http://www.nasa.gov/images/content/542391main_iss027e012224_full.jpg",
"http://www.nasa.gov/images/content/542198main_IMG_0128-raw_full.jpg"
);
//
// reference to our http instance
private var _http:mdm.HTTP = null;
// index to track current state
private var currentIndex:int = 0;
// called as entry point - e.g. after mdm.Application.init is called
//
public function initApp():void
{
progressBar.mode = "manual";
callLater(downloadFile);
};
//
// this method is called to get another file from queue
//
public function downloadFile():void
{
// sanity check
if(currentIndex == images.length)
{
return;
};
// reset progressBar
progressBar.indeterminate = true;
progressBar.label = "";
// get next url
var url:String = images[currentIndex] as String;
// get filename from url
var filename:String = getFilenameFromURL(url);
progressBar.label = "loading "+filename;
// prevent caching
url = addRandomID(url);
// and compute local path
var localPath:String = mdm.Application.path+filename;
// if previously downloaded remove it -
// though it is not really required as it will be fully overwritten
var exists:Boolean = mdm.FileSystem.fileExists(localPath);
if(exists)
{
mdm.FileSystem.deleteFile(localPath);
};
// now get file in the background
this.http.getFile(url, "", "", localPath);
};
// handlers
//
// when file is downloaded update current index
// update progressBar and use "filename" from
// event object to load local into image component
//
private function onBinaryTransferComplete(event:Object):void
{
currentIndex++;
var path:String = event.filename;
image.source = path;
progressBar.indeterminate = true;
progressBar.label = path+" loaded";
callLater(downloadFile);
};
private function onError(event:Object):void
{
// not implemented to
};
//
// on progress update progressBar
//
private function onProgress(event:Object):void
{
if(progressBar.indeterminate) progressBar.indeterminate = false;
progressBar.setProgress(event.bytesTransferred, event.bytesTotal);
};
//
private function get http():mdm.HTTP
{
if(!_http)
{
_http = new mdm.HTTP();
_http.onBinaryTransferComplete = onBinaryTransferComplete;
_http.onError = onError;
_http.onProgress = onProgress;
};
return _http;
}
// the local filename is computed (extracted) from
// url of image
private function getFilenameFromURL(url:String):String
{
var filename:String = null;
if(url && url.length > 0)
{
var tokens:Array = url.split("/");
filename = tokens.pop() as String;
};
return filename;
};
// to prevent caching add random url parameter string
private function addRandomID(path:String):String
{
return path+"?__id__="+(new Date().getTime());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment