Flex中使用RemoteObject实现图片上传功能
最近看到一个通过BlazeDS中的RemoteOjbect通讯方式来进行上传图片功能,感觉不错,网上大部分都是用flex + java servlet?来实现的。如:http://www.riameeting.com/node/210 而这个感觉更简单,所以就贴了出来,希望能方便大家: ? 客户端: <?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" ???????????????currentState="pre_upload"> ????<fx:Script> ?????????<![CDATA[ ??????????? ?????????????import?flash.events.Event; import?flash.net.FileFilter; import?flash.net.FileReference; import?mx.controls.Alert; import?mx.events.CloseEvent; import?mx.managers.PopUpManager; import?mx.rpc.events.FaultEvent; import?mx.rpc.events.ResultEvent; ????????????? private?var?fileRef:FileReference =?new?FileReference(); private?function?pickFile(evt:MouseEvent):void ?????????????{ ?????????????????var?imageTypes:FileFilter =?new?FileFilter("图片?(*.jpg,*.jpeg,*.gif,*.png)",?"*.jpg; *.jpeg; *.gif; *.png"); var?allTypes:Array =?new?Array(imageTypes); ?????????????????fileRef.addEventListener(Event.SELECT,selectHandler); ?????????????????fileRef.addEventListener(ProgressEvent.PROGRESS,progressHandler); ?????????????????fileRef.addEventListener(Event.COMPLETE,completeHandler); ?????????????????try{ ?????????????????????fileRef.browse(allTypes); ?????????????????????}catch?(error:Error){ ?????????????????????????trace("Unable to browse for files."+error.toString()); ????????????????????} ?????????????} function?progressHandler(evt:ProgressEvent):?????????????????lb_progress.text =?"?已上传?"?+ (evt.bytesLoaded/1024).toFixed(2)+?" K,共?"+ (evt.bytesTotal/1024).toFixed(2) +?" K"; var?proc: uint = evt.bytesLoaded / evt.bytesTotal * 100; ?????????????????progress.setProgress(proc,100); ?????????????????progress.label=?当前进度: "?+?" "?+ proc +?"%"; function?selectHandler(evt:Event):?????????????????currentState =?"uploading"; ?????????????????fileRef.load(); function?completeHandler(evt:Event):void{ "post_upload"; ?????????????????upload.uploadFile(fileRef.data,fileRef.name); ?????????????????image_post_upload.source = fileRef.data; function?returnUpLoadHandler():????????????????currentState =?"pre_upload"; ?????????????]]> ?????????</fx:Script> ?????<fx:Declarations> ????????????<s:RemoteObject?id="upload" destination="uploadFile" endpoint="../messagebroker/amf"/> ????????????</fx:Declarations> ?????<s:states> ????????????<s:State?name = "pre_upload"/> uploading"/> post_upload"/> ????????????</s:states> ?????<s:Button?includeIn="pre_upload" label="从电脑上上传图片" width="119" click="pickFile(event)" horizontalCenter="0" y="64"/> ?????<mx:ProgressBar?id="progress" includeIn="uploading" x="43" y="43" width="153"/> ?????<s:Label?id="lb_progress" includeIn="45" y="10" width="149" height="25"/> lb_post_upload" includeIn="post_upload" x="108" y="21" width="124" height="32" fontSize="16" text="upload success!" fontFamily="Arial" color="#3374DE"/> 159" y="60" label="返回" click="returnUpLoadHandler()"/> ?????<mx:Image?id="image_post_upload" includeIn="10" y="67" height="67"/> ? </s:Application> 服务器: package?com.service; import?java.io.File; import?java.io.FileOutputStream; public?class?UploadFile { ????void?uploadFile(byte[] content,String fileName)?throws?Exception { ???????File file =?new?File("d:"?+ fileName); ???????FileOutputStream stream =?new?FileOutputStream(file); ???????if?(content !=?null) ???????????stream.write(content); ???????stream.close(); ????} } RemoteObject-config.xml配置文件: <destination id="uploadFile"> ????????<properties> ????????????<source>com.service.UploadFile</source> ????????</properties> ????</destination> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |