Tag Archive for URLLoader

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

AS3 Loading and Using an External CSS File

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class CSSFormattingExample extends Sprite {
		var loader:URLLoader;
		var field:TextField;
		var exampleText:String = "<h1>This is a headline</h1>" +
		"<p>This is a line of text. <span class='bluetext'>" +
		"This line of text is colored blue.</span></p>";

		public function CSSFormattingExample():void {
			field = new TextField();
			field.width=300;
			field.autoSize=TextFieldAutoSize.LEFT;
			field.wordWrap=true;
			addChild(field);

			var req:URLRequest=new URLRequest("example.css");

			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onCSSFileLoaded);
			loader.load(req);
		}

		public function onCSSFileLoaded(event:Event):void {
			var sheet:StyleSheet = new StyleSheet();
			sheet.parseCSS(loader.data);
			field.styleSheet=sheet;
			field.htmlText=exampleText;
		}
	}
}


// THE 'example.css' FILE SHOULD LOOK LIKE THIS ...
//p {
//	font-family: Times New Roman, Times, _serif;
//	font-size: 14;
//}
//
//h1 {
//	font-family: Arial, Helvetica, _sans;
//	font-size: 20;
//	font-weight: bold;
//}
//
//.bluetext {
//	color: #0000CC;
//}

source

Loader builder utility function (URLLoader & Loader)

private var _loaderformat:String;
private var _loadURL:String;
private var _loadertype:String;
//////////////////////////////
//URL Loader builder.
//Text/Binary & SWF/Image objects.
//////////////////////////////
public function urlLoaderBuilder (_loadURL, _loaderformat = 'text', _loadertype = 'urlloader') {
var req:URLRequest = new URLRequest(_loadURL);
switch(_loadertype) {
case 'urlloader':
var urlloader:URLLoader = new URLLoader();
urlloader.load (req);
switch(_loaderformat) {
case 'text':
urlloader.dataFormat = URLLoaderDataFormat.TEXT;
break;
case 'variables':
urlloader.dataFormat = URLLoaderDataFormat.VARIABLES;
break;
case 'binary':
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
break;
}
return urlloader;
break;
case 'loader':
var loader:Loader = new Loader();
loader.load (req);
return loader;
break;
}
}
//////////////////////////////

source

comunicazione con un Web Server

var variables:URLVariables = new URLVariables();
variables.miavariabile = "Ciao";
var request:URLRequest = new URLRequest();
request.url = "http://www.miodominio.com/miapagina.php";
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completeHandler);
try {
loader.load(request);
} catch (error:Error) {
trace("Errore nel caricamento dell' URL");
}
function completeHandler(event:Event):void {
var x_xml:XML = XML(event.target.data);
trace(x_xml);
}

source