Skip to content

Instantly share code, notes, and snippets.

@peterblazejewicz
Created May 11, 2011 14:35
Show Gist options
  • Save peterblazejewicz/966558 to your computer and use it in GitHub Desktop.
Save peterblazejewicz/966558 to your computer and use it in GitHub Desktop.
downloading file into plugin with copy saved into local drive
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="800" height="600"
applicationComplete="mdm.Application.init(this);">
<fx:Script>
<![CDATA[
import mdm.Application;
import mdm.FileSystem;
import mx.controls.Alert;
//
private var loader:URLLoader = null;
//
// start screen
// when user clicks button switch state
// to "progress" and start downloading remote file
//
protected function getRemoteImageHandler(event:MouseEvent):void
{
event.target.enabled = false;
currentState = "progress";
progressBar.indeterminate = true;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(ProgressEvent.PROGRESS, progressEventHandler);
loader.addEventListener(Event.COMPLETE, completeEventHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorEventHandler);
var request:URLRequest = new URLRequest("http://www.nasa.gov/images/content/542384main_image_1940_800-600.jpg");
request.data = {id:(new Date().getTime())};
loader.load(request);
};
//
// progress screen - update progress bar respectively to download progress
// data is downloaded into flash runtime memory directly
//
protected function progressEventHandler(event:ProgressEvent):void
{
if(progressBar.indeterminate) progressBar.indeterminate = false;
progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
};
//
// image state - when image is fully downloaded (complete fired)
// get reference to underlying byte array and write it to local file
// then load it into image loader instance to show to user
//
protected function completeEventHandler(event:Event):void
{
// first work with data
var ba:ByteArray = loader.data as ByteArray;
var path:String = mdm.Application.path+"myImage.jpg";
// if exists delete to be sure of results
var exists:Boolean = mdm.FileSystem.fileExists(path);
if(exists)
{
mdm.FileSystem.deleteFile(path);
}
mdm.FileSystem.BinaryFile.setDataBA(ba);
mdm.FileSystem.BinaryFile.writeDataBA(path);
//
ba = null;
loader.removeEventListener(ProgressEvent.PROGRESS, progressEventHandler);
loader.removeEventListener(Event.COMPLETE, completeEventHandler);
loader.removeEventListener(IOErrorEvent.IO_ERROR, errorEventHandler);
loader = null;
//
currentState = "showImage";
image.load(path);
};
//
// simple error handler
//
protected function errorEventHandler(event:IOErrorEvent):void
{
loader.removeEventListener(ProgressEvent.PROGRESS, progressEventHandler);
loader.removeEventListener(Event.COMPLETE, completeEventHandler);
loader.removeEventListener(IOErrorEvent.IO_ERROR, errorEventHandler);
loader = null;
Alert.show(event.text, "Error");
};
]]>
</fx:Script>
<!-- !states -->
<s:states>
<s:State name="start"/>
<s:State name="progress"/>
<s:State name="showImage"/>
</s:states>
<!-- !ui -->
<s:Button x="10" y="10" label="Get remote image" width="780" includeIn="start" click="getRemoteImageHandler(event)"/>
<mx:Image includeIn="showImage" width="100%" height="100%" id="image"/>
<mx:ProgressBar includeIn="progress" horizontalCenter="0" verticalCenter="0" width="80%" labelPlacement="top" id="progressBar"/>
</s:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment