Posted By

alvincrespo on 03/01/10


Tagged

file mp3 as3 Reference actionscript3 filereference


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

leecsargent
bruzed
zoo
tspitzr
burnandbass
mamadashi


Using the FileReference API in AS3 for Uploading MP3's


 / Published in: ActionScript 3
 

This is the file reference class ive been using for one of my projects

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

Report this snippet  

You need to login to post a comment.