下面有个小小的例子:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
public class Browse extends Sprite
{
private var file:FileReference=new FileReference();
private var loader:Loader=new Loader();
private function onClick(event:MouseEvent):void
{
var f:FileFilter=new FileFilter
("Images", "*.jpg;*.gif;*.png");
file.browse([f]);
file.addEventListener(Event.SELECT,onSelect);
}
private function onSelect(event:Event):void
{
file.load();
file.addEventListener(Event.COMPLETE,onComplete);
file.removeEventListener(Event.SELECT,onSelect);
}
private function onComplete(event:Event):void
{
file.removeEventListener(Event.COMPLETE,onComplete);
loader.loadBytes(file.data);
loader.contentLoaderInfo.
addEventListener(Event.COMPLETE,onLoadComplete);
}
private function onLoadComplete(event:Event):void
{
var tempData:BitmapData=new BitmapData
(loader.width,loader.height,false);
tempData.draw(loader);
var bitmap:Bitmap=new Bitmap(tempData);
bitmap.y=50;
this.addChild(bitmap);
loader.contentLoaderInfo.removeEventListener(
Event.COMPLETE,onLoadComplete);
}
public function Browse()
{
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
var button:TextField=new TextField();
button.autoSize=TextFieldAutoSize.CENTER;
button.text="打开文件";
button.border=true;
this.addChild(button);
button.addEventListener(MouseEvent.CLICK,onClick);
}
}
}