Flex上傳本地圖片并提前瀏覽的實(shí)現(xiàn)方法
更新時(shí)間:2014年01月14日 15:02:50 作者:
個(gè)性頭像最終需要上傳到服務(wù)器的文件系統(tǒng)中,但是程序希望在用戶選擇后直接有個(gè)預(yù)覽,針對(duì)這個(gè)問(wèn)題,下面有個(gè)不粗的實(shí)現(xiàn),希望對(duì)大家有所幫助
經(jīng)常會(huì)設(shè)計(jì)一個(gè)這樣的功能,比如更改個(gè)性頭像,這個(gè)個(gè)性頭像最終需要上傳到服務(wù)器的文件系統(tǒng)中,但是程序希望在用戶選擇后直接有個(gè)預(yù)覽,然后用戶才進(jìn)行上傳。這個(gè)功能技術(shù)上其實(shí)就是需要對(duì)本地的文件能進(jìn)行讀取。在flash player10中有個(gè)類(lèi)FileReference的類(lèi)可以實(shí)現(xiàn)這個(gè)功能,而實(shí)現(xiàn)對(duì)文件讀取的接口是load( )函數(shù),要注意的是:
a、這個(gè)函數(shù)只能在UI操作中使用,比如用戶按下按鈕。
b、加載進(jìn)來(lái)后的本地文件無(wú)法在AS中使用
c、這個(gè)接口是一個(gè)異步的過(guò)程,也就不是馬上就加載進(jìn)來(lái),需要加Listener來(lái)操作。
下面是參考代碼
<?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);//增加當(dāng)打開(kāi)瀏覽文件后,用戶選擇好文件后的Listener
}
private function browseHandler(event:Event):void {
fr.browse([imageTypes]);//打開(kāi)瀏覽文件的dialog
}
private function selectHandler(event:Event):void {
fr.addEventListener(Event.COMPLETE, onLoadComplete);//增加一個(gè)文件加載load完成后的listener
fr.load(); //加載用戶選中文件
}
private function onLoadComplete(e:Event):void
{
imgPhoto.source = fr.data;
}
]]>
</fx:Script>
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
</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、這個(gè)函數(shù)只能在UI操作中使用,比如用戶按下按鈕。
b、加載進(jìn)來(lái)后的本地文件無(wú)法在AS中使用
c、這個(gè)接口是一個(gè)異步的過(guò)程,也就不是馬上就加載進(jìn)來(lái),需要加Listener來(lái)操作。
下面是參考代碼
復(fù)制代碼 代碼如下:
<?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);//增加當(dāng)打開(kāi)瀏覽文件后,用戶選擇好文件后的Listener
}
private function browseHandler(event:Event):void {
fr.browse([imageTypes]);//打開(kāi)瀏覽文件的dialog
}
private function selectHandler(event:Event):void {
fr.addEventListener(Event.COMPLETE, onLoadComplete);//增加一個(gè)文件加載load完成后的listener
fr.load(); //加載用戶選中文件
}
private function onLoadComplete(e:Event):void
{
imgPhoto.source = fr.data;
}
]]>
</fx:Script>
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
</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>
相關(guān)文章
Flex動(dòng)態(tài)生成可編輯的DataGrid具體實(shí)現(xiàn)代碼
DataGrid具有以下功能:表頭是動(dòng)態(tài)生成的、每行都是有序號(hào)的、每行都是可以編輯、插入、刪除、修改,接下來(lái)為大家分享下Flex如何動(dòng)態(tài)生成可編輯的DataGrid2013-04-04Flex DataGrid自動(dòng)編號(hào)示例
這篇文章主要介紹了Flex DataGrid如何自動(dòng)編號(hào),感興趣的朋友可以參考下2014-05-05flex中使用RadioButtonGroup時(shí)取出所選項(xiàng)的值的方法
flex中的RadioButtonGroup想必大家并不陌生吧,在本文將為大家介紹下在使用RadioButtonGroup時(shí)如何取出所選項(xiàng)的值,感興趣的朋友可以參考下2013-12-12Flex tree加虛線顯示效果并且替代原始圖標(biāo)
Flex tree修改默認(rèn)圖標(biāo)并且加虛線顯示效果,實(shí)在是看不下去那種巨丑無(wú)比的小箭頭+文件夾的顯示方式,具體實(shí)現(xiàn)如下,有此需求的朋友可以參考下,希望對(duì)家有所幫助2013-08-08Flex 基于數(shù)據(jù)源的Menu Tree實(shí)現(xiàn)代碼
由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接,在源數(shù)據(jù)上加href和target屬性來(lái)控制打開(kāi)窗口,可自定義父節(jié)點(diǎn)和子節(jié)點(diǎn)圖標(biāo),不設(shè)置采用系統(tǒng)默認(rèn),感興趣的你可以了解下啊,或許對(duì)你有所幫助2013-01-01flex導(dǎo)出excel具體實(shí)現(xiàn)
flex導(dǎo)出excel的前提是需要插件as3xls-1.0.1.swc,下面為大家介紹下具體的實(shí)現(xiàn)2014-01-01Flex中TabNavigator設(shè)置Tabs樣式思路及源碼
這篇文章主要介紹了Flex中TabNavigator如何設(shè)置Tabs樣式有哪些思路,感興趣的朋友可以看看下面的源碼2014-05-05獲取到AdvancedDataGrid選中行的全部數(shù)據(jù)
通過(guò)AdvancedDataGrid的id來(lái)獲取selectedItem和selectedItems屬性,下面有個(gè)不不錯(cuò)的示例大家可以參考下2014-02-02