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

Posted

tags:

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

/*
	
	Author: Alvin Crespo
	Date: 3/1/2010
	
	

*/
package acrespo.classes.managers{
	//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 acrespo.classes.constants.ProgramConstants;
	import acrespo.classes.MainDocument;
	
	//utils
	import acrespo.classes.constants.ProgramConstants;
	
	public class FileManager {
		
		private var _MainDoc:MainDocument; //reference to document class
		private var cFileReference:FileReference = new 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")]);
		}
		
		public function uploadFiles():void {
			trace("\n\nUploading File: " + cFileReference.name);
				
			var request:URLRequest = new URLRequest();
				request.url = cUploadURL;
			trace("\n\nPHP Location: " + cUploadURL +"\n");
				
			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);
			//load sound
			trace("\nLOAD SOUND");
			_MainDoc._SoundManager.GetMusicData(cFileReference.name);
		}
		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 使用AS3中的FileReference API上传MP3的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3中的getURL

ActionScript 3 AS3中的模糊滤镜

ActionScript 3 跟踪AS3中的对象

ActionScript 3 检查AS3中的连接

ActionScript 3 Flex中的AS3 Sprites

ActionScript 3 AS3中的按钮模板类