The following example loads and displays the
data found in a local text file. It also traces event handling information.
Note:To run this example, put a file named example.txt
in the same directory as your SWF file. That file should be a simple text file containing
a few words or lines of text.
The example code does the following:
- The constructor function creates a URLLoader instance named
loader
. - The
loader
object is passed to the configureListeners()
method,
which adds listeners for each of the supported URLLoader events. - A URLRequest instance named
request
is created, which specifies name of the file to be loaded. - The
method
property of the request is set to URLRequestMethod.POST
. - The
request
object is then passed to loader.load()
, which loads the text file. - When the URLLoader has finished loading the text file the
Event.COMPLETE
event fires,
triggering the completeHandler()
method. The completeHandler()
method simply traces
the data
property, the contents of the text file.
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;
public class URLRequestMethodExample extends Sprite {
private var loader:URLLoader;
public function URLRequestMethodExample() {
loader = new URLLoader();
configureListeners(loader);
var request:URLRequest = new URLRequest("example.txt");
request.method = URLRequestMethod.POST;
loader.load(request);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
}
}