Filereference.download creation must be outside function – WTF?!

I was pulling my hair out yesterday - hair what am I going on about - I'm a baldy! anyway, I have an application that runs on FP9 so needs to use the Filereference.download() function by uploading the file to the server via a php script and then downloading it from the server via Filereference.download(). I was doing all my creating within a function, so:

Actionscript:
  1. private function downloadFile():void
  2. {
  3.     var fr:FileReference = new FileReference();
  4.     fr.addEventListener(Event.COMPLETE, downloadDone);
  5.     fr.download(new URLRequest("http://somewebsite.com/uploadedfile.csv"), "savedfile.csv");
  6. }

For ages I couldn't get any events to fire from it or even the file to download and found it very frustrating and so started stripping everything back and moving things around and as soon as I moved the 'var fr:FileReference' to the beginning of the file outside the constructor it worked perfectly - very strange and couldn't find anything in the Adobe documentation about this - or am I missing something? Lets hope the next project targets FP10 at least so I can just use the save() function instead!

This entry was posted in Actionscript 3.0 and tagged . Bookmark the permalink.

7 Responses to Filereference.download creation must be outside function – WTF?!

  1. michael keating says:

    isn’t this just a basic scope issue, the listener event function gets fired after the download (which could have come from any file reference object), the event.target then needs to find the originating filereference object but that was only defined within the scope of the private function – hence should report a null ?

    I tried something very interesting yesterday, NewfileReference.load() after the browse and select from local, then newLoaderObject.loadBytes(NewfileReference.data) as the load comes back as a byteArray, and after the loader has it, you can then work with it.

    What this accomplishes is to load say a very large image file directly into the browser from local file storage without an upload time penalty. I have never really looked at this before, always previously loading to the server first and then once uploaded loading to the browser.

  2. UltraVisual says:

    Nope the listeners are specific to that FileReference instance, I also added all the other listeners available (progress, securityError, IOError, Select, cancel, open) – not a single one fired – no references to the event were called in the trace statements to the relevant event, just return strings to trace, but still nothing :(
    Nothing like this has to be done for any other reference to any other object which has a listener added so why is Filerefence so special?

  3. Shawn says:

    This is basic actionscript memory management.

    Any variable defined within a function, gets removed from memory as soon as that function closes.

    You object was not dispatching events because your object no longer exists.

    You might think that your listener should keep it in memory, but this is a common misunderstanding of how listeners work. When you add an even listener, the target object gets a reference to the callback function, and thus keeps the functions class in memory. It doesn’t work the other way arround.

    For instance, inside of MyClass, if I add:
    timer.addEventListener(“Event”, someFunction);

    The timer is actually keeping “MyClass” in memory. But nothing is keeping the timer itself in memory (if it hasn’t been declared as a class level variable)…

    This isn’t specific to FileReference either, it happens with many different types of objects.

  4. May says:

    Mate, that’s bad style, addEventListner on local vars. Removing the listener gets cumbersome then. If you don’t pay attention you create a memory leak.

  5. UltraVisual says:

    Ok maybe so, but I have had no problem creating an object within a function before and listeners still manage to fire without any problems, surely calling the download function of the filereference class keeps the class in memory? also what is strange is that doing any of this in FP9 causes this problem, but if it is done in FP10 it works perfectly??!!

  6. UltraVisual says:

    Hi May, I can assure you that I am totally religious about removing my listeners and reducing the risks of memory leaks as I have encountered issues due to this in the past and nothing teaches you better than your own mistakes!

  7. Addendum to last comment: I just had an almost exact same problem with a download, and then I remembered I had a similar problem before a couple of years ago – SECURITY – the file download must be fired from a button event to stop evil ppl from trying to download naughty files.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>