加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex上传和下载文件

发布时间:2020-12-15 03:38:49 所属栏目:百科 来源:网络整理
导读:flex上传和下载文件主要通过 FileReference类即可实现。 下载文件比较简单: public var fileRef:FileReference = new FileReference();....添加各种监听fileRef.addEventListener() {Event.COMPLETE Event.CANCEL ProgressEvent.PROGRESS 等}...fileRef.d

flex上传和下载文件主要通过FileReference类即可实现。

下载文件比较简单:

public var fileRef:FileReference = new FileReference();
....
添加各种监听
fileRef.addEventListener() {Event.COMPLETE  Event.CANCEL  ProgressEvent.PROGRESS 等}
...
fileRef.download(downloadURL,fileName);

然而,由于flash player的安全限制,download处会报出Error #2176的错误,因此需要通过触发鼠标或按钮事件来进行下载。

例如:

<pre name="code" class="java">public function getURLBack(url:String):void{
	Alert.show("确定下载该文件?","下载选择",Alert.YES|Alert.NO,viewHandle,function(e:CloseEvent):void{alertClickHandler(e,url)});
}
private function alertClickHandler(evt:CloseEvent,url:String):void {
	if (evt.detail == Alert.YES) {
		var downLoadURL:URLRequest = new URLRequest(url);
		fileRef = new FileReference();
		...
		添加各种监听
		...
		try{
			fileRef.download(downLoadURL,fileName);
		}catch(errObject:Error) {
			Alert.show("文件下载失败!n 原因:"+errObject.message);
		}
	}
}

 

这样就不会catch到Error #2176的错误。可参考:https://forums.adobe.com/message/2848670 或?http://bbs.csdn.net/topics/350075799


上传文件相对复杂些,这里有个实例(前端)为多文件上传载,且每个上传的文件分别带有进度条,可作为参考:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="left" verticalAlign="top" width="600" height="450" applicationComplete="initApp()">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import flash.events.MouseEvent;
			import flash.events.ProgressEvent;
			import mx.controls.Alert;
			public var test:int;
			
			public var arr:ArrayCollection= new ArrayCollection();
			
			public var arr2:ArrayCollection = new ArrayCollection();//存放为被上传的东西,与显示无关
			var myFileRef:FileReferenceList = new FileReferenceList();
			
			public var array:Array=new Array();//用户可能分几次选择,将每次选择的内容放入数组中,点击上传后一起上传
			
			var urlRequest:URLRequest = new URLRequest("http://www.beijibear.com/flashupload.php");
			private function initApp():void{
				myFileRef.addEventListener(Event.SELECT,selectHandler);//监听上传对话框确定按钮
				//myFileRef.addEventListener(Event.CANCEL,cancelHandler);//监听上传对话框取消按钮
			}
			private function selectHandler(event:Event):void {//上传框确认按钮按下
				for(var i:int=0;i<myFileRef.fileList.length;i++){
					var f:FileReference = FileReference(myFileRef.fileList[i]); 
					var obj:Object = new Object();
					obj.fileRefrence = f;//与进度条绑定
					obj.fileName =f.name;
					obj.fileSize =(f.size/1024).toFixed(2)+"KB";//toFixed(num)四舍五入到小数点后几位
					obj.type = f.type;
					arr.addItem(obj);
			        arr2.addItem(obj);
				}
				dataGrid.dataProvider=arr;
			}
			private function uploadCompleteHandler(event:Event):void {//删除已上传文件在数组中的信息
				for each(var item in arr2)
				{
					if(item.fileRefrence==event.currentTarget)
					{
						arr2.source.indexOf(item);//找到已经上传的文件,返回下标
						var index:Number = arr2.source.indexOf(item);
						arr2.removeItemAt(index);//删除已经上传的文件
					}
				} 
	        }
			private function select(event:MouseEvent):void{//浏览按钮按下
				myFileRef.browse();
			}
			//通过urlrequest对象创建一个与服务端的连接,然后直接调用fielreference类的upload()方法
			private function onUpload(event:Event):void{
				for(var i:int=0;i<arr2.length;i++){
					var f:FileReference = FileReference(arr2[i]['fileRefrence']); 
			        f.upload(urlRequest);
			        f.addEventListener(Event.COMPLETE,uploadCompleteHandler);
				}
			}
			private function onCancel():void{
				arr.removeAll();
				arr2.removeAll();
				dataGrid.dataProvider=arr;
			}
		]]>
	</mx:Script>
	<mx:Panel width="550" height="400" title="Upload" horizontalAlign="center" verticalAlign="middle">
		<mx:DataGrid width="500" height="250" id="dataGrid">
			<mx:columns>
				<mx:DataGridColumn headerText="文件名称" dataField="fileName" fontSize="12"/>
				<mx:DataGridColumn headerText="文件大小" dataField="fileSize" fontSize="12"/>
				<mx:DataGridColumn headerText="文件类型" dataField="type" fontSize="12"/>
				<mx:DataGridColumn width="130" headerText="状态" fontSize="12">
					<mx:itemRenderer>
					  <mx:Component>
					    <mx:HBox width="300" paddingLeft="2" horizontalGap="2" verticalAlign="middle">
					     <mx:ProgressBar id="progress" width="100%" 
					      minimum="0" maximum="100" source="{data.fileRefrence}" 
					      labelPlacement="center"
					      label="%3%%">
					     </mx:ProgressBar>
					     <!--<mx:LinkButton width="15" label="取消" click="cancel(event)" icon="@Embed('_.png')">
					     	<mx:Script>
					     		<![CDATA[
					     			private function cancel(event:MouseEvent):void{
					     				data.fileRefrence.cancel();
								        
         								progress.label = "已取消";
					     			}
					     		]]>
					     	</mx:Script>
					     </mx:LinkButton>-->
					     <mx:LinkButton width="13" label="删除" click="deleteItem(event)" icon="@Embed('x.png')" >
					     	<mx:Script>
					     		<![CDATA[
							        import mx.collections.ArrayCollection;
							        import mx.controls.DataGrid;
							        import mx.controls.Alert;
							        private function deleteItem(event:MouseEvent):void {
								         var grid:Object = event.target.parent.parent.parent;
								         var dp:ArrayCollection = ArrayCollection(grid.dataProvider);
								         var index:int = dp.getItemIndex(data);
								         outerDocument.arr2.removeItemAt(index);
								         dp.removeItemAt(index); 
								         grid.parent.refresh();
							        }
					     		]]>
					     	</mx:Script>
					     </mx:LinkButton>
					    </mx:HBox>
					  </mx:Component>
					</mx:itemRenderer>
				</mx:DataGridColumn>
			</mx:columns>
		</mx:DataGrid>
		<mx:Canvas width="424" height="45">
			<mx:Button label="浏览……" id="browse" right="118" click="select(event)" height="23" y="13" width="67" fontSize="12"/>
			<mx:Button label="上传" id="up" right="62" click="onUpload(event)" y="12" fontSize="12"/>
			<mx:Button label="取消" id="cancel" right="6" y="12" fontSize="12" click="onCancel()"/>	
		</mx:Canvas>
	</mx:Panel>
</mx:Application>
此段转自: http://www.beijibear.com/index.php?aid=78

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读