AS3 File Reference Manager Class using the File Reference API


/ Published in: ActionScript 3
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  1. package {
  2. //events
  3. import flash.errors.IOError;
  4. import flash.events.DataEvent;
  5. import flash.events.Event;
  6. import flash.events.HTTPStatusEvent;
  7. import flash.events.IOErrorEvent;
  8. import flash.events.SecurityErrorEvent;
  9. //net
  10. import flash.net.FileFilter;
  11. import flash.net.FileReference;
  12. import flash.net.URLRequest;
  13. import flash.net.URLLoader;
  14. import flash.net.URLRequestMethod;
  15. import flash.net.URLLoaderDataFormat;
  16. import flash.net.URLVariables;
  17.  
  18. //custom classes
  19. import ProgramConstants;
  20.  
  21. public class FileManager {
  22.  
  23. private var _MainDoc:MainDocument; //reference to document class
  24. private var cFileReference:FileReference;
  25. private var cUploadURL = ProgramConstants.SERVER_URL+ProgramConstants.PHP_URL+ProgramConstants.UPLOAD_URL; //url of php file
  26.  
  27. public function FileManager(pMainDoc:MainDocument){
  28. trace("File Manager Instantiated");
  29.  
  30. _MainDoc = pMainDoc;
  31. cFileReference = new FileReference();
  32.  
  33. //EVENTS
  34. //Dispatched when the user selects a file for upload or download from the file-browsing dialog box.
  35. cFileReference.addEventListener(Event.SELECT, fileReferenceSelect);
  36.  
  37. //Dispatched when an upload or download operation starts.
  38. cFileReference.addEventListener(Event.OPEN, fileReferenceOpen);
  39.  
  40. //Dispatched when download is complete or when upload generates an HTTP status code of 200.
  41. cFileReference.addEventListener(Event.COMPLETE, uploadcomplete);
  42.  
  43. //Dispatched after data is received from the server after a successful upload.
  44. //This event is not dispatched if data is not returned from the server.
  45. cFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileReferenceData);
  46.  
  47. //ERROR EVENTS
  48. //Dispatched when an upload fails and an HTTP status code is available to describe the failure.
  49. cFileReference.addEventListener(HTTPStatusEvent.HTTP_STATUS, fileReferenceHTTPError);
  50.  
  51. //Dispatched when the upload or download fails.
  52. cFileReference.addEventListener(IOErrorEvent.IO_ERROR, fileReferenceIOError);
  53.  
  54. //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.
  55. cFileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileReferenceSecurityError);
  56. }
  57.  
  58. public function browseFiles():void {
  59. /*
  60. *
  61. * FileFilter(description:String, extension:String, macType:String = null);
  62. * Indicates what files on the user's system are shown in the file-browsing dialog box that is displayed when the FileReference.browse() method
  63. *
  64. * */
  65. cFileReference.browse([new FileFilter("Only MP3 Formats","*.mp3;*.wmv")]);
  66. }
  67.  
  68. public function uploadFiles():void {
  69. trace("Upload File: " + cFileReference.name );
  70.  
  71. var request:URLRequest = new URLRequest();
  72. request.url = cUploadURL;
  73. trace(cUploadURL);
  74.  
  75. cFileReference.upload(request);
  76. }
  77.  
  78. /*
  79. *
  80. * Event Handlers
  81. *
  82. * */
  83. private function fileReferenceSelect(e:Event):void{
  84. trace("Chosen File: " + cFileReference.name);
  85. _MainDoc.inputContainer.uploadinput.sampledata.text = String(cFileReference.name);
  86. }
  87. private function fileReferenceOpen(e:Event):void {
  88. trace("File Upload Started");
  89. }
  90. private function uploadcomplete(e:Event):void {
  91. trace("File Upload Complete");
  92. }
  93. private function fileReferenceData(e:DataEvent):void {
  94. trace("Data from Server: " + e.data);
  95. }
  96. private function fileReferenceHTTPError(e:HTTPStatusEvent):void {
  97. trace("HTTP Status Error: " + e.status);
  98. }
  99. private function fileReferenceIOError(e:IOErrorEvent):void {
  100. trace("IOErrorEvent: " + e.toString());
  101. }
  102. private function fileReferenceSecurityError(e:SecurityErrorEvent):void {
  103. trace("Secruity Error: " + e.text);
  104. }
  105. }
  106. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.