ActionScript 3 使用文件参考API的AS3文件参考管理器类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 使用文件参考API的AS3文件参考管理器类相关的知识,希望对你有一定的参考价值。

package {
	//events
	import flash.errors.IOError;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	//net  
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLVariables;
	
	//custom classes
	import ProgramConstants;
	
	public class FileManager {
		
		private var _MainDoc:MainDocument; //reference to document class
		private var cFileReference:FileReference;
		private var cUploadURL = ProgramConstants.SERVER_URL+ProgramConstants.PHP_URL+ProgramConstants.UPLOAD_URL; //url of php file
		
		public function FileManager(pMainDoc:MainDocument){
			trace("File Manager Instantiated");
			
			_MainDoc = pMainDoc;
			cFileReference = new FileReference();
			
			//EVENTS
			//Dispatched when the user selects a file for upload or download from the file-browsing dialog box.
			cFileReference.addEventListener(Event.SELECT, fileReferenceSelect);
			
			//Dispatched when an upload or download operation starts.
			cFileReference.addEventListener(Event.OPEN, fileReferenceOpen);
			
			//Dispatched when download is complete or when upload generates an HTTP status code of 200.
			cFileReference.addEventListener(Event.COMPLETE, uploadcomplete);
			
			//Dispatched after data is received from the server after a successful upload.
			//This event is not dispatched if data is not returned from the server.
			cFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileReferenceData);
			
			//ERROR EVENTS
			//Dispatched when an upload fails and an HTTP status code is available to describe the failure.
			cFileReference.addEventListener(HTTPStatusEvent.HTTP_STATUS, fileReferenceHTTPError);
			
			//Dispatched when the upload or download fails.
			cFileReference.addEventListener(IOErrorEvent.IO_ERROR, fileReferenceIOError);
			
			//Dispatched when a call to the FileReference.upload() or FileReference.download() method tries to upload a file to a server or get a file from a server that is outside the caller's security sandbox.
			cFileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileReferenceSecurityError);
		}
		
		public function browseFiles():void {
			/*
			 * 
			 * FileFilter(description:String, extension:String, macType:String  = null);
			 * 		Indicates what files on the user's system are shown in the file-browsing dialog box that is displayed when the FileReference.browse()  method
			 * 
			 * */
			cFileReference.browse([new FileFilter("Only MP3 Formats","*.mp3;*.wmv")]);
		}
		
		public function uploadFiles():void {
			trace("Upload File: " + cFileReference.name );
				
			var request:URLRequest = new URLRequest();
				request.url = cUploadURL;
			trace(cUploadURL);
				
			cFileReference.upload(request);
		}
		
		/*
		 * 
		 * Event Handlers
		 * 
		 * */
		private function fileReferenceSelect(e:Event):void{
			trace("Chosen File: " + cFileReference.name);
			_MainDoc.inputContainer.uploadinput.sampledata.text = String(cFileReference.name);
		}
		private function fileReferenceOpen(e:Event):void {
			trace("File Upload Started");
		}
		private function uploadcomplete(e:Event):void {
			trace("File Upload Complete");
		}
		private function fileReferenceData(e:DataEvent):void {
			trace("Data from Server: " + e.data);
		}
		private function fileReferenceHTTPError(e:HTTPStatusEvent):void {
			trace("HTTP Status Error: " + e.status);
		}
		private function fileReferenceIOError(e:IOErrorEvent):void {
			trace("IOErrorEvent: " + e.toString());
		}
		private function fileReferenceSecurityError(e:SecurityErrorEvent):void {
			trace("Secruity Error: " + e.text);
		}
	}
}

以上是关于ActionScript 3 使用文件参考API的AS3文件参考管理器类的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 使用AS3中的FileReference API上传MP3

ActionScript 3 没有extrenal API的简单缓和

ActionScript 3 Python:Last.fm简单API示例

ActionScript 3 AS3曲线绘图API和Glow

ActionScript 3 AS3绘图API线条绘图和DropShadow

ActionScript 3 使用ActionScript 3异步读取文件