2
private var csv:URLLoader = new URLLoader();
private var array:Array = new Array();
private var urlr:URLRequest = new URLRequest();
public function loadRecipe(path:String):void
{
    try
    {
        csv.dataFormat = URLLoaderDataFormat.TEXT;
        urlr = new URLRequest(path);
        csv.addEventListener(Event.COMPLETE, finishRecipe);     
        csv.load(urlr);
    }
    catch (e:SecurityErrorEvent)
    {
        trace("1");
    }
    catch (e:IOErrorEvent)
    {
        trace("2");
    }
}

public function finishRecipe(e:Event):void
{
    var csvString:String = csv.data as String;
    array = csvString.split(",");
}

My code that I'm working with is above. I can't get the completion event to ever trigger, that is, my array is never populated. Can anyone give me insight as to why?

EDIT: I changed to get rid of all the weak references and check for errors. I don't get any errors.

  • Code looks okay to me. Have you tried handling IOErrorEvent.IO_ERROR and SecurityErrorEvent.SECURITY_ERROR? – net.uk.sweet Jan 18 '13 at 00:51
  • Yeah, I edited the code to check for those errors, I don't seem to get them. I also tried getting rid of all my weak references, but I still do not get the event to trigger. – user1964603 Jan 18 '13 at 14:29
  • That's not the way to handle IOEvent and SecurityErrorEvent. You have to use addEventListener to handle these events, as you're doing with COMPLETE. – Jonathan Lidbeck Apr 24 '14 at 03:34

2 Answers2

2

I've run into this bug frequently over the years. When certain browsers (FireFox, Chrome) retrieve the file from cache instead of network, the loader will dispatch a PROGRESS event but never COMPLETE.

Try clearing your browser cache and see whether the file loads correctly next time. If so, you can do one of two things as a workaround:

  1. Break the cache by adding a random string to the end of your request URL.

    urlr = new URLRequest(path + "?cachebust=" + Math.floor(100000+900000*Math.random()));
    

    This is simple to code, but has obvious disadvantages, forcing unnecessary reloads.

  2. Listen for both COMPLETE and PROGRESS events. In the PROGRESS handler, check to see if bytesLoaded matches bytesTotal. If it does, remove all handlers and continue as if it were a COMPLETE event.

    ... somewhere in your code ...
        loader.addEventListener(Event.COMPLETE, handleComplete);
        loader.addEventListener(ProgressEvent.PROGRESS, handleProgress);
    
    ... somewhere else
    private function handleProgress(evt:ProgressEvent):void
    {
        checkLoadComplete();
    }
    private function handleComplete(evt:Event):void
    {
        checkLoadComplete();
    }
    private function checkLoadComplete():void
    {
        if(loader.bytesTotal > 0 && loader.bytesLoaded == loader.bytesTotal) {
            loader.removeEventListener(Event.COMPLETE, handleComplete);
            loader.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
    
            ... your code here
        }
    }
    
Jonathan Lidbeck
  • 1,555
  • 1
  • 14
  • 15
  • I had this issue like half of time even with adding a random query string to the address. It is probably related to the servers caching proxy and repeat body data. In any case, progress workaround worked fully. Thanks – Eddie Jul 06 '15 at 19:09
0

Ummm just looking at the code you provided it seems like you actually try to catch the errors using try/catch. In order to find errors, you have to start listening to them on the actual loader. Like this:

public function Foobar() {
    var loader:URLLoader;
    loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
    loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
    loader.addEventListener(Event.COMPLETE, onComplete);
}

private function onComplete(e:Event):void {}
private function onProgress(e:ProgressEvent):void {}
private function onSecurityError(e:SecurityErrorEvent):void {}
private function onIOError(e:IOErrorEvent):void {}
Daniel MesSer
  • 1,191
  • 1
  • 7
  • 13
  • I added the event listeners for the security and IO errors and their respective functions with traces to see if they ever get executed. I still did not catch any errors. – user1964603 Jan 18 '13 at 20:02
  • Well, then the only option is to run the debug player and see if there is some "in-between-error". Which seems higly unlikely since you are loading a txt-file... – Daniel MesSer Jan 18 '13 at 20:05
  • Similar to this question: http://stackoverflow.com/questions/3263166/handle-verifyerror-error-1014-when-loading-swfs-using-as3 – Daniel MesSer Jan 18 '13 at 20:06