flex上传和下载文件
发布时间:2020-12-15 05:06:59 所属栏目:百科 来源:网络整理
导读:?xml version="1.0" encoding="utf-8"?mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" initialize="init()"mx:Script![CDATA[import mx.controls.Alert;import flash.net.FileReference;public var myFileReference:FileRef
<?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" initialize="init()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import flash.net.FileReference; public var myFileReference:FileReference = new FileReference(); private function init():void { process.label = "0 byte"; showDetail.text = "无文件上传"; } private function browse():void { //浏览本地文件 myFileReference.browse(); //为打开选择框定义处理 myFileReference.addEventListener(Event.SELECT,selectHandler); //在上传过程中触发的事件处理 myFileReference.addEventListener(ProgressEvent.PROGRESS,onProcess); myFileReference.addEventListener(Event.COMPLETE,onComplete); } private function selectHandler(event:Event):void { sName.text = myFileReference.name; } private function upload():void { //调用upload.jsp接受从flsh player发送的文件 var request:URLRequest = new URLRequest("http://localhost:8080/UpAndDownloadFile/upload.jsp"); try{ //上传文件 myFileReference.upload(request); showDetail.text = "upload"; }catch(error:Error) { mx.controls.Alert.show("上传文件出错"); } } private function onProcess(event:ProgressEvent):void { var proc:uint = event.bytesLoaded; var total:uint = event.bytesTotal; process.setProgress(proc,total); showDetail.text = "上传中"; process.label = proc.toString()+"bytes"; } private function onComplete(event:Event):void { showDetail.text = "上传结束"; sName.text = ""; this.parentDocument.download.init(); } //取消事件 private function cancel():void { myFileReference.cancel(); } ]]> </mx:Script> <mx:Button x="330" y="41" label="浏览" click="browse()"/> <mx:TextInput id="sName" x="26" y="39" width="272"/> <mx:Button x="26" y="81" label="上传" click="upload()"/> <mx:Button x="110.5" y="81" label="取消" click="cancel()"/> <mx:ProgressBar id="process" x="26" y="172" width="360" mode="manual" labelPlacement="right"/> <mx:Label x="26" y="125" text="当前状态"/> <mx:TextInput id="showDetail" x="110" y="125" width="188"/> </mx:Canvas> <?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" width="414" height="316"> <mx:Script> <![CDATA[ import mx.collections.XMLListCollection; import mx.rpc.events.ResultEvent; import mx.controls.Alert; public var filePath:String; public var myFilereference:FileReference = new FileReference(); public function init():void { getList.send(); process1.label = "0 bytes"; } //下载文件 private function down():void { var u:String = "http://localhost:8080/UpAndDownloadFile/down.jsp?file="+filePath; var request:URLRequest = new URLRequest(u); myFilereference.download(request); myFilereference.addEventListener(ProgressEvent.PROGRESS,onProcess); myFilereference.addEventListener(Event.COMPLETE,onComplete); } private function onProcess(event:ProgressEvent):void { var proc:uint = event.bytesLoaded; var total:uint = event.bytesTotal; process1.setProgress(proc,total); process1.label = proc.toString()+" bytes"; } private function resultHandler(event:ResultEvent):void { var x:XML = new XML(event.result); fileList.dataProvider = x.item; getList.request.someValue = Math.random(); } private function listChange():void { var fName:String = fileList.selectedItem.toString(); fN.text = fName; filePath = "/apace/"+fName; } private function onComplete(event:Event):void { mx.controls.Alert("下载完成"); } ]]> </mx:Script> <mx:HTTPService id="getList" url="http://localhost:8080/UpAndDownloadFile/List.jsp" result="resultHandler(event)" resultFormat="xml" method="POST"> <mx:request xmlns=""> <someValue> {Math.random()} </someValue> </mx:request> </mx:HTTPService> <mx:List id="fileList" x="10" y="34" height="232" width="184" change="listChange()"> </mx:List> <mx:Label x="10" y="8" text="可下载文件"/> <mx:Label x="217" y="34" text="从列表中选择要下载的文件"/> <mx:Label id="fN" x="217" y="60" text="文件名"/> <mx:LinkButton x="362" y="176" label="下载" click="down()"/> <mx:ProgressBar id="process1" x="202" y="238" mode="manual"/> </mx:Canvas> <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" fontSize="14"> <mx:TabNavigator id="ud" x="155" y="102" width="445" height="400"> <!--上传模块--> <local:uploadFile id="up" label="上传文件"/> <local:downloadFile id="download" label="下载文件"/> </mx:TabNavigator> </mx:Application> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |