Actionscript:
-
var txt:TextField = TextField(addChild(new TextField()));
-
txt.autoSize = TextFieldAutoSize.LEFT;
-
txt.x = txt.y = 20;
-
txt.text = "click anywhere to load an image file...";
-
-
var fileRef:FileReference= new FileReference();
-
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
-
function onDown(evt:MouseEvent):void{
-
fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
-
fileRef.addEventListener(Event.SELECT, onSelected);
-
stage.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
-
}
-
function onSelected(evt:Event):void{
-
fileRef.addEventListener(Event.COMPLETE, onLoaded);
-
fileRef.load();
-
fileRef.removeEventListener(Event.SELECT, onSelected);
-
}
-
function onLoaded(evt:Event):void{
-
var loader:Loader = new Loader();
-
loader.loadBytes(evt.target.data);
-
addChild(loader);
-
fileRef.removeEventListener(Event.COMPLETE, onLoaded);
-
}
This snippet shows how to use the FileReference.load() method to load an image into flash player RAM and display it on the stage.