Loading text files using the URLLoader class in ActionScript 3.0

by Peter deHaan on February 26, 2008

in LoadVars, URLLoader, migration

The following example shows how you can load an external text files using ActionScript 2.0 and ActionScript 3.0.

Full code after the jump.

In ActionScript 2.0, you can use the LoadVars class and the onData event handler to load an external text file, as seen in the following example:

// ActionScript 2.0
var PATH:String = "lorem.txt";
var loadVars:LoadVars = new LoadVars();
loadVars.onData = function(str:String):Void {
    if (str) {
        textArea.text = str;
    } else {
        // FAIL
        textArea.text = "Unable to load text file: " + PATH;
    }
}
loadVars.load(PATH);

In ActionScript 3.0, you use the URLRequest and URLLoader classes, and then listen for the complete event, as seen in the following example:

// ActionScript 3.0
var PATH:String = "lorem.txt";
var urlRequest:URLRequest = new URLRequest(PATH);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT; // default
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);
 
function urlLoader_complete(evt:Event):void {
    textArea.text = urlLoader.data;
}

{ 7 comments… read them below or add one }

1 Mitch 01.06.09 at 9:14 pm

okay, so how can i get the urlLoader.data into a variable for use in other functions? this problem has been bugging me for ~6 hours.

2 Bhavya Technologies 02.08.09 at 9:54 am

What is the content should be there in “lorem.txt” file.

website design hyderabad

3 Roshan 03.03.09 at 9:05 pm

Hi Peter,

Thanks for the example. It worked and I’m modifying the code for advanced text search.

Regards,
Roshan

4 Ali Rathore 04.16.09 at 3:49 am

b1.addEventListener(MouseEvent.MOUSE_UP,fetchData);
function fetchData(event:Event):void {

// ActionScript 3.0
var PATH:String = “Vision.txt”;
var urlRequest:URLRequest = new URLRequest(PATH);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT; // default
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);

function urlLoader_complete():void {
this.txt.text = urlLoader.data;
}

}

it gives the error

TypeError: Error #1010: A term is undefined and has no properties.
at MethodInfo-1()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

5 Anonymous 04.19.09 at 3:08 pm

this is braindead!

6 Anonymous 06.27.09 at 6:40 am

Very useful thanks!

7 Nancy 12.09.09 at 6:21 am

I found your code to load an external text file in actionscript 3.0. I am quite new to this but I did everything you mentioned in your post. As you can see below, here is my code.

First I have my imports at the top:
import flash.net.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;

Then i created the variables:

private var path:String = “undergraduate.txt”;
private var url:URLRequest = new URLRequest(path);
private var urlLoader:URLLoader = new URLLoader();

Then in my function to open a popup i inserted the following:

urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlloader_complete);
urlLoader.load(url);

here is the code for the entire function:

private function PopIt1(e:MouseEvent):void {

urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlloader_complete);
urlLoader.load(url);

var h1:Object = new Object();
h1.fontWeight = “bold”;
h1.color = “#000000″;
h1.fontSize = 20;
h1.leading = 10;
h1.fontFamily = “Arial”;

var p:Object = new Object();
p.fontWeight = “bold”;
p.color = “#000000″;
p.fontSize = 13;
p.fontFamily = “Arial”;

var style:StyleSheet = new StyleSheet();
style.setStyle(“h1″, h1);
style.setStyle(“p”, p);

txtUndergradDescription = new TextField();

txtUndergradDescription.x=10, txtUndergradDescription.y=13;
txtUndergradDescription.width=550,txtUndergradDescription.height=300;
txtUndergradDescription.wordWrap = true;
txtUndergradDescription.selectable = true;
txtUndergradDescription.styleSheet = style;

txtUndergradDescription.htmlText = urlLoader;

/*txtUndergradDescription.htmlText = “Undergraduate Medical Education”;
txtUndergradDescription.htmlText = txtUndergradDescription.htmlText + “Quisque non purus lectus. Curabitur at pellentesque quam. Donec suscipit leo sit amet magna sodales eu laoreet odio varius. Aliquam faucibus libero vitae sapien convallis imperdiet. Nunc lacinia justo in quam suscipit ac elementum sem sodales. Proin a euismod enim. Vestibulum vel dui quis libero adipiscing scelerisque at a massa. Nulla eleifend libero at ligula vulputate non porta lectus placerat. Maecenas sagittis gravida neque non tincidunt. Aenean nec diam justo. Nullam eu quam ligula. Quisque tristique tristique dui sodales elementum. Proin dictum interdum tincidunt. Fusce volutpat enim eu nisl sodales fringilla in non dolor. Praesent mauris risus, porttitor a semper nec, fermentum quis risus. Quisque iaculis luctus enim, sit amet lobortis mi sodales id. Duis lacus eros, feugiat id pulvinar a, tincidunt nec diam. Integer in metus orci. Donec id lacus arcu.”;
*/

mc_pop1=new mcPopup();
mc_pop1.x=0.6,mc_pop1.y=0.6;
mc_pop1.width=573.5,mc_pop1.height=248.8;

TransitionManager.start(mc_pop1, {type:Zoom, direction:Transition.IN, duration:1, easing:Bounce.easeOut});

addChild(mc_pop1);
mc_pop1.addChild(txtUndergradDescription);

}

Than I added the your function called urlloader_complete:

private function urlloader_complete(evt:Event):void {

txtUndergradDescription.text = urlLoader;

}

And it keeps giving me the type was not found or was not a compile-time constant : Event

Seems to me it does not recognize Event

I tried to put MouseEvent instead

And then I get this error:

1067: Implicit coercion of a value of type flash.net:URLLoader to an unrelated type String.

What am I doing wrong?

I really am confused, perhaps it is just a stupid thing, but I can’t figure it out.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: