cnapsys
cnapsys

Reputation: 121

AS3 event listeners inside loop

I'm having a bit of a hard time figuring this one out.

I'm creating an Air/Flash app that uploads files from a local directory to the server.(without user interaction). I'm creating an Array of the files found in the folder and can upload them fine without any issues. However, I can not keep track of the event listeners; meaning, the for loop obviously completes before the event listeners for each upload.

Is there a way to keep track of the event listeners and find out when the last one finished?

Any help is greatly appreciated...

Here's my code:

public var UPLOAD_URL:String = "http://myhost/dev/uptest.php";
upbtn.addEventListener(MouseEvent.CLICK, uploadme);
public function uploadme(event:MouseEvent):void
    {
        preloader.visible = true;
        var desktop:File = File.applicationStorageDirectory.resolvePath("photo");
        var files:Array = desktop.getDirectoryListing();
        for (var i:uint = 0; i < files.length; i++)
        {
            var date:Date = new Date(); // get new date
            var mytime:Number = Math.round(date.valueOf()/1000);
            var ur:URLRequest = new URLRequest();
            var uv:URLVariables = new URLVariables();
            uv.filename = files[i].name;
            ur.data = uv;
            ur.method = URLRequestMethod.POST;
            ur.url = UPLOAD_URL;

            files[i].addEventListener(ProgressEvent.PROGRESS, updateProgress);
            files[i].addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, doneUpload);
            files[i].addEventListener(IOErrorEvent.IO_ERROR, fileError);
            files[i].upload(ur);
            //trace(i)

            function updateProgress(event:ProgressEvent):void
            {
                //wait preloader.visible = true;
                //trace(event.currentTarget.name);
            }
            function doneUpload(event:DataEvent):void
            {
                //var loader2: URLLoader = URLLoader(event.target);
                //trace(i);

                trace(event.data);
                //preloader.visible = false;
            }

            function fileError(event:IOErrorEvent):void
            {
                trace("error");
            }               
        }
    }

Upvotes: 1

Views: 119

Answers (1)

Brian
Brian

Reputation: 3914

One approach is to keep track of how many files you're uploading and only act in the doneUpload listener once all files have finished:

private var numFiles:int = 0;
public function uploadme(event:MouseEvent):void
{
    ...
    var files:Array = desktop.getDirectoryListing();
    numFiles = files.length;
    for (var i:uint = 0; i < files.length; i++)
    ...


}

function doneUpload(event:DataEvent):void
{
    //var loader2: URLLoader = URLLoader(event.target);
    //trace(i);

    trace(event.data);

    numFiles--;

    if (numFiles == 0) {
        trace("All the files are done uploading.");
    }

    //preloader.visible = false;
}

Upvotes: 2

Related Questions