<?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"
creationComplete="init()"
minWidth="955" minHeight="600">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import flash.net.*;
import mx.controls.*;
import mx.utils.*;
// 图片预览开始
private var fileReference:FileReference = new FileReference();
private var byteArray:ByteArray;
private var bitmapData:BitmapData;
private var loader:Loader=new Loader();
private function init():void
{
fileReference.addEventListener(Event.COMPLETE,fileReference_completeHandler);
}
private function fileReference_completeHandler(e:Event):void
{
byteArray = fileReference.data;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loader_completeHandler);
loader.loadBytes(byteArray);
}
private function loader_completeHandler(event:Event):void
{
var bitmap:Bitmap = Bitmap(loader.content);
bitmapData = bitmap.bitmapData;
img.source = bitmap;
Alert.show(ObjectUtil.toString(fileReference.data));
}
private function browse_clickHandler(event:MouseEvent):void
{
var imgArr:Array = [];
imgArr.push(new FileFilter("图片(*.gif;*.jpg;*.png)", "*.gif;*.jpg;*.png"));
fileReference.browse(imgArr);
fileReference.addEventListener(Event.SELECT,fileReference_selectHandler);
}
private function fileReference_selectHandler(event:Event):void
{
fileReference.load();
}
// 图片预览结束
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:HGroup x="-5" y="10" width="440" height="319">
<s:Button label="选择图片" id="browse" click="browse_clickHandler(event)"/>
<mx:Image id="img" width="357" height="314"/>
</s:HGroup>
</s:Application>