使用AS3中的FileReference API上载MP3&ŧ039;s

Posted

tags:

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

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(" Uploading File: " + cFileReference.name);
  83.  
  84. var request:URLRequest = new URLRequest();
  85. request.url = cUploadURL;
  86. trace(" PHP Location: " + cUploadURL +" ");
  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(" LOAD 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. }

以上是关于使用AS3中的FileReference API上载MP3&ŧ039;s的主要内容,如果未能解决你的问题,请参考以下文章

如何通过fileReference从本地上传的mp3中提取id3标签? (AS3)

完整的文件参考不能在 as3 中工作

ASP.NET Web API,从 Flex FileReference 上传时 MIME 多部分流意外结束

在 AS3 图形 API 上应用 16 位颜色

crossdomain.xml 和 FileReference.upload 的 Flex 问题

请问AS3.0 怎样可以将Bitmap对象保存为本地图片文件(如JPG文件)。谢谢!