狮子座男

导航

Flex + .Net从本地选择一个图片上传到服务器

<mx:TextInput id="TxtFileName" editable="false" width="200"/>
<mx:Image id="btnBrowser" toolTip="Browser" buttonMode="true" click="fileBrowser(event)"                    
source="@Embed(source='../assets/icon/icon_browser.png')"/>
<mx:Button id="btnUpload" label="Upload" buttonMode="true" click="addAttachment(event)" skin="@Embed(source='../assets/skin/btn_skin.gif')" width="80" height="23" />

新建一个 textiput存储显示文件名和image来做button选取本地图片文件。

触发的事件代码

private var curfile:FileReference=new FileReference(); 
private var browerFlag:Boolean=false;

private
function fileBrowser(event:Event):void { var imageTypes:FileFilter = new FileFilter("Images(*.jpg,*.jpeg,*.png,*.gif,*.bmp,*.pdf,*.tif)", "*.jpg;*.jpeg;*.png;*.gif;*.bmp;;*.pdf;*.tif"); curfile.browse([imageTypes]); }

程序initialize的时候为curfile加监听事件initialize="init()

private function init():void
{
  curfile.addEventListener(Event.SELECT,addInTextinput);
}
private function addInTextinput(event:Event):void
{
  TxtFileName.text = curfile.name;
}

 Button Upload的处理事件

            private function addAttachment(event:Event):void
            {            
                if(!browerFlag)
                {
                    return;
                }
                if(!curfile.type)
                {
                    browerFlag=false;
                    TxtFileName.text="";
                    Alert.show(CONSTANT.VoidFileType,CONSTANT.PROJNAME,Alert.OK);
                    return;
                }
                var type:String=curfile.type.toLowerCase();
                if(type&&type!='.jpg'&&type!='.png'&&type!='.bmp'&&type!='.gif'&&type!='.pdf'&&type!='.tif')
                {
                    browerFlag=false;
                    TxtFileName.text="";
                    Alert.show(CONSTANT.VoidFileType,CONSTANT.PROJNAME,Alert.OK);
                    return;
                }
                /* if(curfile.size>4194304) */
                if(curfile.size>10485760)
                { 
                    browerFlag=false;
                    TxtFileName.text="";
                    Alert.show(CONSTANT.AttachmentMax,CONSTANT.PROJNAME,Alert.OK);
                    return;
                }

                //---upload handler--------------
                var url:String = "UploadFileHandler.aspx?TempFileID="+SysParm.expInfo.ExpenseGUID+"&StaffID="+SysParm.curStaffInfo.StaffID;
                var reqUrl:URLRequest = new URLRequest(url);
                
                curfile.upload(reqUrl);    
        
            }

怎加两个上传结束以及捕捉错误的事件监听。

            private function init():void
            {
                curfile.addEventListener(Event.SELECT,addInTextinput);
                curfile.addEventListener(Event.COMPLETE,uploadComplete);
                curfile.addEventListener(IOErrorEvent.IO_ERROR,uploadError);
            }    
            private function uploadComplete(event:Event):void
            {    
                browerFlag=false;
                ExpenseFunction.removeBusy();
                Alert.show(CONSTANT.UploadSuccess,CONSTANT.PROJNAME,Alert.OK,null,refreshAttachmentHandler);                
            }    
            private function uploadError(ioError:IOErrorEvent):void
            {
                browerFlag=false;
                TxtFileName.text="";
                Alert.show(CONSTANT.UploadFail,CONSTANT.PROJNAME);
            }
            
            private function refreshAttachmentHandler(event:CloseEvent):void
            {
                TxtFileName.text="";
                browerFlag=false;

            }

 

posted on 2014-01-10 11:24  狮子座男  阅读(282)  评论(0编辑  收藏  举报