AS3 Load External Text File


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



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.events.Event;
  5. import flash.events.IOErrorEvent;
  6. import flash.net.URLLoader;
  7. import flash.net.URLRequest;
  8.  
  9. public class Main extends MovieClip
  10. {
  11. private var _txtFileToLoad:String = "myTextFile.txt";
  12. private var _txtLoader:URLLoader;
  13. private var _loadedTxt:String;
  14.  
  15.  
  16. public function Main():void
  17. {
  18. loadExternalTxtFile();
  19. }
  20.  
  21. private function loadExternalTxtFile():void
  22. {
  23. _txtLoader = new URLLoader();
  24. _txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
  25. _txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
  26. _txtLoader.load(new URLRequest(_txtFileToLoad));
  27. }
  28.  
  29. private function onTxtLoader_COMPLETE(event:Event):void {
  30. _txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
  31. _txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
  32. _loadedTxt = String(event.target.data);
  33. trace(_loadedTxt);
  34. }
  35.  
  36. private function onTxtLoader_IO_ERROR(event:Event):void {
  37. _txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
  38. _txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
  39. trace(event.target.text);
  40. }
  41.  
  42. }
  43. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.