2008-03-10

flex 上传文件

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12" creationComplete="init()" width="518" height="347">
    
        <mx:Script>
         <![CDATA[
             
             import flash.net.FileReference;
            import flash.net.URLRequest;
            import flash.net.FileFilter;
            import flash.net.URLVariables;
            
            import flash.events.ProgressEvent;
            
            import mx.managers.CursorManager;
             //
             private var file:FileReference;
                          
             internal function init():void{

                 file = new FileReference();   
                 file.addEventListener(Event.SELECT, onSelect);  
                file.addEventListener(ProgressEvent.PROGRESS, processHandler);  
                
                file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadDataHandler);
                
             }
             
             internal function doSelect():void{
                 var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.png)", "*.jpg;*.jpeg;*.png");  
                var allTypes:Array = new Array(imageTypes);  
                 file.browse(allTypes);
             }
             internal function onSelect(evt:Event):void{
                 pic_txt.text = file.name;
             }
             internal function doUpload():void{
                 var request:URLRequest = new URLRequest("upload.php");
                                  
                //request.data = new URLVariables();
                file.upload(request);
                CursorManager.setBusyCursor();
             }
             internal function processHandler(evt:ProgressEvent):void{
                //
                //evt.bytesTotal;
             }        
             internal function uploadDataHandler(evt:DataEvent):void{
                 CursorManager.removeBusyCursor(); 
                 
                 img.source = evt.data.toString();
             }
         ]]>
     </mx:Script>

    <mx:Image id="img" width="377" height="108" x="10" y="59"/>    
    <mx:TextInput id="pic_txt" width="150" x="109" y="10"/>
    <mx:Button label="选择文件" click="doSelect()" x="10" y="10"/>
    <mx:Button label="开始上传" click="doUpload()" x="295" y="10"/>

</mx:Application>


原文链接:http://www.fluidea.cn/blog/index.php/2007/12/19/44/
演示地址:http://www.fluidea.cn/book/uploadEx/FlexUpload.html
评论
发表评论

您还没有登录,请登录后发表评论

ccmv
搜索本博客
最近加入圈子
存档
最新评论