avorum
avorum

Reputation: 2293

AS3: URLLoader.load is failing to load data into the URLLoader.data property

I am trying to have my program able to read in data from a .csv and display the contents in a specific fashion. I am trying to use the URLLoader class to do so. The following is the code my function for reading in the file

//Instantiate the URLLoader and set up the exception handling associated with it
var loader:URLLoader = new URLLoader();
loader.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );
loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleHttpStatus );
loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR,  handleSecurityError );
loader.addEventListener( Event.COMPLETE, handleComplete );

//Next load the options file that will have location of .csv file
loader.dataFormat = URLLoaderDataFormat.TEXT
loader.load(new URLRequest("testData.csv"));
trace("data: " + loader.data);

Running this code results in the trace printing "data: undefined", upon debugging I saw that the URLLoader.load wasn't actually changing any of the fields in loader. My understanding was that this method was supposed to put the loaded data into the data property. If anyone has any ideas as to what could be causing the error I would be extremely grateful.

Upvotes: 1

Views: 1315

Answers (2)

CGBe
CGBe

Reputation: 183

This is how you should do it programmatically, it's basically the code format of Torious's words mentioned above. Good luck!

var myTextLoader:URLLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.load(new URLRequest("myText.txt"));

function onLoaded(e:Event):void {
    trace(e.target.data);
}

Upvotes: 0

Torious
Torious

Reputation: 3424

Your code accessing the data property should reside in the handleComplete function, because the load() function is asynchronous; it returns immediately, but its actual execution is done in parallel. Once loading is complete, the appropriate event handler is called (handleComplete in your case).

Upvotes: 5

Related Questions