Revision: 24272
Updated Code
at February 25, 2010 00:58 by alvincrespo
Updated Code
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); } } }
Revision: 24271
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 25, 2010 00:54 by alvincrespo
Initial Code
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.MUSIC_FOLDER; //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; 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); } } }
Initial URL
Initial Description
Based on the following references: http://not-equal.blogspot.com/2006/08/upload-file-as3-php.html http://www.thedanosphere.com/?p=76 I created a simple file manager class that I will be extending in the near future for an AIR App I will also be releasing. If you have more that could be added, let me know and I would be glad to make modifications. Enjoy my fellow actionscriptrs!
Initial Title
AS3 File Reference Manager Class using the File Reference API
Initial Tags
file, actionscript, api
Initial Language
ActionScript 3