Loading an external text file with ActionScript 3
To load a text file with ActionScript, you only need few lines of code. I'm gonna present both, a short way and a valid way of doing it.
In order to load an external text file, you need to use URLLoader, which is designed for loading text files, XMLs or binary data. We can't use a Loader class, which was designed to import only Flash supported files like swf, jpg, png or gif. As usual with any kind of loading classes, we also need to use URLRequest which identifies the location of the file and initiates download. We are going to listen for Event.COMPLETE to know when Flash finishes loading our text file. Also, if file is bigger we can listen for ProgressEvent to get the number of bytes loaded and create the preloader (for more info about preloading read article: Two ways of preloading in ActionScript 3).
Below is the code to quickly import a text file and trace it. I created a text file called mytextfile.txt and placed it in the same folder where the swf is.
1 |
var myLoader:URLLoader = new URLLoader(new URLRequest("mytextfile.txt")); |
In the output box, you should see printed content of your text file. Pretty easy, isn't it?
OK, as promised, now the long way, sometimes called also "the proper way". First, some sources say that you should actually define an event listener before loading a text file, as in some cases, text file might load first before Flash actually starts to listen for a Complete event, so you won't know if the file was loaded or not. I don't know if that's true, anyway, Adobe in theirs examples also add listeners first...
Also it's advisable to handle any errors thrown due to problems with loading the files. In order to do that, you need to first use try...catch block to catch the errors and then add an event listener to get information about the errors. Without a listener, Flash would print the error to the output window, but since you also want to get information about error when using swf from the website, you should print it to the text field (output is only available when testing in Flash Application). Note that you can't just add listener to listen for error events without try...catch block as there would be nothing to listen for - no event will be thrown. You won't even know if the file was successfully loaded or not, maybe only from the blank text field...
Anyway, this is how I would do that in a proper way. In my project, I'm trying to load the same file - mytextfile.txt that is in the same folder and print its content to the TextField. Next, to test errors catching, I going to remove that text file and check if Flash catches the error.
1 |
package |
If everything is ok and your text file is in correct place you should see its content printed in the TextField.
If you remove the text file or change its name and test your project again, you should see this message in the Text Field:
There was a problem loading a mytextfile.txt file: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: file:///D|/eu/sierakowski/samples/txtloader/mytextfile.txt"]
The other types of error that you may encounter are the security errors; this would be the case when your text file is not in the same place but on different web server and domain... But this is a subject for another article :)
If your text file is huge (you try to load a whole book :) or you load binary data), you may want to use preloader. If so, please read this article: Two ways of preloading in ActionScript 3.


Comments