November 04, 2008

AIR FileReference and scope

I ran into a small problem recently in my AIR application when using FileReference to initiate and monitor a download operation; no events from a FileReference object created within a method were being fired. After a quick check of the Livedoc entry I found the issue:

if the FileReference object goes out of scope, any upload or download that is not yet completed on that object is canceled upon leaving the scope. Be sure that your FileReference object remains in scope for as long as the upload or download is expected to continue.

So basically a FileReference object needs to be created in such a way that it stays in scope for the duration of your operation; if you instantiate it within a method it there’s a good chance it will go out of scope and any events you were waiting for from it will never fire. The easiest solution is to declare it outside of the method, so instead of:

private function download(event:MouseEvent):void 
    var fileRef:FileReference = new FileReference();
    fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    fileRef.addEventListener(Event.COMPLETE, onComplete);
you need to do something like this:

private var fileRef:FileReference();
private function download(event:MouseEvent):void 
    fileRef = new FileReference();
    fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    fileRef.addEventListener(Event.COMPLETE, onComplete);

Sorted. Afterwards I also found more info about this via a couple of useful blog articles here and here.

- 4 comments by 1 or more people Not publicly viewable

  1. daman

    Hi, well i am looking for adding Progress event to file rather that file reference, as i am copying all the files from the directory selected rather that multiple or individual files.
    I am using the following.
    dirsel.browseForDirectory(“Select Directory to Import Files”)
    Please help, i am geting the event.complete correctly, it fires on when file copy is complete on the file but not progress event.

    05 Nov 2008, 07:34

  2. Steven Carpenter

    Hi Daman,

    Firstly make sure you are using the copyToAsync() method for the copy operation. If you use copyToAsync() you can add listeners for some events. However, even with copyToAsync() standard progress-type events are still not available as you’ve found.

    Fortunately Matthew Poole found a solution to this; he used OutputProgressEvent.OUTPUT_PROGRESS and refactored the standard example so that the CLOSE operation only fires after event.BytesPending is 0. His code means you can watch file write progress as per your requirement Here’s Matthew’s solution in full. Let me know if it works for you. :-)

    05 Nov 2008, 09:14

  3. Peter Every

    Steve – sorry to burst in like this – fancy a pint?


    21 Nov 2008, 22:20

  4. Steven Carpenter

    Ooh yeah, sorry Peter, been a bit busy this end! How about next week?

    21 Nov 2008, 22:47

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.


MXNA link



    Other blogs I like...

    Black Pepper Software

    Eismann-sf Go to 'Comments on: Design News for Web, Graphic Designers'

    Ted On Flex Go to 'Ted On Flash'



    Not signed in
    Sign in

    Powered by BlogBuilder
    © MMXXII