Flex上傳本地圖片并提前瀏覽的實現(xiàn)方法
更新時間:2014年01月14日 15:02:50 作者:
個性頭像最終需要上傳到服務器的文件系統(tǒng)中,但是程序希望在用戶選擇后直接有個預覽,針對這個問題,下面有個不粗的實現(xiàn),希望對大家有所幫助
經常會設計一個這樣的功能,比如更改個性頭像,這個個性頭像最終需要上傳到服務器的文件系統(tǒng)中,但是程序希望在用戶選擇后直接有個預覽,然后用戶才進行上傳。這個功能技術上其實就是需要對本地的文件能進行讀取。在flash player10中有個類FileReference的類可以實現(xiàn)這個功能,而實現(xiàn)對文件讀取的接口是load( )函數(shù),要注意的是:
a、這個函數(shù)只能在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完成后的listener
fr.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>
a、這個函數(shù)只能在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完成后的listener
fr.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>
相關文章
Flex動態(tài)生成可編輯的DataGrid具體實現(xiàn)代碼
DataGrid具有以下功能:表頭是動態(tài)生成的、每行都是有序號的、每行都是可以編輯、插入、刪除、修改,接下來為大家分享下Flex如何動態(tài)生成可編輯的DataGrid2013-04-04flex中使用RadioButtonGroup時取出所選項的值的方法
flex中的RadioButtonGroup想必大家并不陌生吧,在本文將為大家介紹下在使用RadioButtonGroup時如何取出所選項的值,感興趣的朋友可以參考下2013-12-12Flex 基于數(shù)據(jù)源的Menu Tree實現(xiàn)代碼
由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接,在源數(shù)據(jù)上加href和target屬性來控制打開窗口,可自定義父節(jié)點和子節(jié)點圖標,不設置采用系統(tǒng)默認,感興趣的你可以了解下啊,或許對你有所幫助2013-01-01Flex中TabNavigator設置Tabs樣式思路及源碼
這篇文章主要介紹了Flex中TabNavigator如何設置Tabs樣式有哪些思路,感興趣的朋友可以看看下面的源碼2014-05-05獲取到AdvancedDataGrid選中行的全部數(shù)據(jù)
通過AdvancedDataGrid的id來獲取selectedItem和selectedItems屬性,下面有個不不錯的示例大家可以參考下2014-02-02