AS3 Load External Text File

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Main extends MovieClip
	{
		private var _txtFileToLoad:String = "myTextFile.txt";
		private var _txtLoader:URLLoader;
		private var _loadedTxt:String;
		

		public function Main():void
		{
			loadExternalTxtFile();
		}
		
		private function loadExternalTxtFile():void
		{
			_txtLoader = new URLLoader();
			_txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_txtLoader.load(new URLRequest(_txtFileToLoad));
		}
		
		private function onTxtLoader_COMPLETE(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_loadedTxt = String(event.target.data);
			trace(_loadedTxt);
		}
		
		private function onTxtLoader_IO_ERROR(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			trace(event.target.text);
		}

	}
}

source

Comments are closed.