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;

            }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。