Flex上次本地图片并浏览

来源:互联网 发布:蒸腾系数的算法 编辑:程序博客网 时间:2024/06/06 08:40
经常会设计一个这样的功能,比如更改个性头像,这个个性头像最终需要上传到服务器的文件系统中,但是程序希望在用户选择后直接有个预览,然后用户才进行上传。这个功能技术上其实就是需要对本地的文件能进行读取。在flash player10中有个类FileReference的类可以实现这个功能,而实现对文件读取的接口是load( )函数,要注意的是: 
a、这个函数只能在UI操作中使用,比如用户按下按钮。
b、加载进来后的本地文件无法在AS中使用 
c、这个接口是一个异步的过程,也就不是马上就加载进来,需要加Listener来操作。
下面是参考代码
<?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" minWidth="955" minHeight="600"   creationComplete="creationCompleteHandler(event)"><fx:Script><![CDATA[import flash.net.FileReference;import flash.net.FileFilter;import flash.events.IOErrorEvent;import flash.events.Event;private var fr:FileReference;private var imageTypes:FileFilter;private function creationCompleteHandler(event:Event):void {fr = new FileReference();imageTypes = new FileFilter("Images (*.jpg, *.jpeg, *.png, *.gif)","*.jpg; *.jpeg; *.png; *.gif;")fr.addEventListener(Event.SELECT, selectHandler);//增加当打开浏览文件后,用户选择好文件后的Listener}private function browseHandler(event:Event):void {fr.browse([imageTypes]);//打开浏览文件的dialog}private function selectHandler(event:Event):void {fr.addEventListener(Event.COMPLETE, onLoadComplete);//增加一个文件加载load完成后的listenerfr.load(); //加载用户选中文件}private function onLoadComplete(e:Event):void{imgPhoto.source = fr.data;}]]></fx:Script><s:layout><s:BasicLayout/></s:layout><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><mx:Image id="imgPhoto" visible="true" autoLoad="true" width="1000" height="500"/><mx:Button id="btnBrowse" label="Browse" click="browseHandler(event)" /></s:Application>



原创粉丝点击