flex 上傳圖片

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.                 layout="vertical" 
  4.                 verticalAlign="middle" 
  5.                 horizontalAlign="center" 
  6.                 xmlns:ns1="module.*" 
  7.                 width="800" 
  8.                 verticalScrollPolicy="off" height="648.63635"
  9.     
  10.     <mx:Style>
  11.         global {
  12.             fontSize : 12;
  13.         }
  14.     </mx:Style>
  15.     
  16.     <mx:Script>
  17.         <![CDATA[ 
  18.                     
  19.            private var yourphotoname:String ;
  20.         
  21.            private var file:FileReference = new FileReference();
  22.            
  23.            [Bindable]
  24.            private var stateText:String ; 
  25.             
  26.             protected override function createChildren():void {
  27.                 super.createChildren();
  28.                 file.addEventListener(Event.SELECT, fileselectHandler);
  29.                 //file.addEventListener(Event.COMPLETE, file_complete);
  30.                 file.addEventListener(ProgressEvent.PROGRESS, fileprogressHandler);
  31.                 file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA ,uploadCompleteHandler) ;
  32.             } 
  33.              
  34.             /**************文件上傳完成***************/
  35.             private function uploadCompleteHandler(e:DataEvent):void{
  36.                 stateText = e.data ;
  37.                 imageeditID.photoURL = e.data ;
  38.                 tabbar1.selectedIndex = 1 ; 
  39.                 //filenameID.text = "" ;
  40.             } 
  41.               
  42.             /**************文件上傳完成***************/
  43.             private function fileselectHandler (e:Event):void {
  44.                 filenameID.text = file.name;
  45.                 
  46.             }
  47.             /**************上傳中***************/ 
  48.             private function fileprogressHandler (e:ProgressEvent):void {
  49.                 stateText = Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
  50.             } 
  51.              
  52.             /**************上傳照片***************/ 
  53.             private function upload ():void {
  54.                 if (file.size > 0) { 
  55.                     var request:URLRequest = new URLRequest("http://localhost:8080/uploadservlet");
  56.                     file.upload(request);
  57.                 } 
  58.             }  
  59.         ]]> 
  60.     </mx:Script>    
  61.     <mx:TabBar id="tabbar1" dataProvider="viewstack1" width="760" height="30"/>
  62.     <mx:ViewStack id="viewstack1"  width="750" height="500">
  63.         <mx:Canvas label="上傳照片"  width="100%" height="100%"
  64.             <mx:Button x="140" y="10" label="瀏覽"  click="file.browse();"/> 
  65.             <mx:TextInput  id="filenameID"  x="10" y="10" width="111"/> 
  66.             <mx:Button x="202" y="152" label="上傳" width="78" click="upload();"/>
  67.             <mx:Label x="10" y="154"  text="{stateText}" id="ProgressID"/>
  68.         </mx:Canvas>   
  69.              
  70.         <ns1:imageEdit label="照片編輯" id="imageeditID" width="100%" height="100%">
  71.         </ns1:imageEdit>
  72.     </mx:ViewStack>
  73.     
  74. </mx:Application>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章