AS3/Flex FileReference download problem - solved

I've just spent the last while trying to debug a simple problem (in hindsight) with the FileReference.download() method in a Flex application I'm working on.

If the file reference object is created as a function local variable the download save box appears but the file is never saved.

The solution is documented on the Adobe knowledgbase: FileReference.download() in SWF file fails and I found it via this thread.

The long and short of it is:

Problem code (note the fileRef is inside the function):

function downloadFile():void {
var fileRef:FileReference = new FileReference();
fileRef.download(new URLRequest("myFile.txt"), "myFile.txt");
}
The following example will work correctly:
var fileRef:FileReference = new FileReference();
function downloadFile():void {
fileRef.download(new URLRequest("myFile.txt"), "myFile.txt");
}

Hope it saves someone some time - or increases the chances of the technote being found quickly via google.

Cheers, Mark

Comments
Anand's Gravatar Hmm good stuff dude. thanks for your info.
# Posted By Anand | 7/16/07 5:20 PM
Richard T-J's Gravatar Wish this had been posted 2 weeks ago :-(
I had the same problem and ended up implementing the full example provided in the Flex 3 docs. The weird thing was it seemed to work and the stopped functioning a week later, but it could have been my imagination. At least now I know it wasn't me - exactly :-)

Richard
# Posted By Richard T-J | 7/16/07 5:30 PM
Shaby's Gravatar wow that little info helped me out big time! LOL for a second there I thought I would have to put my files on a direct link.
# Posted By Shaby | 7/19/07 12:02 AM
Andriy's Gravatar you have just saved my life :)
# Posted By Andriy | 7/20/07 3:56 PM
Shekar's Gravatar That saved me a lot of time and pain!!
Thanks to you and Google. Life is easier.:)
# Posted By Shekar | 8/1/07 10:09 PM
stef's Gravatar I HIT THIS and wasted like 3 hours! thank you!!! STEF
# Posted By stef | 8/13/07 5:08 AM
Chris's Gravatar Mark, you are my hero!!!
# Posted By Chris | 8/24/07 12:20 AM
JeffO's Gravatar Thanks! Only wish I'd found your post sooner.
# Posted By JeffO | 8/25/07 7:12 AM
Mark Truluck's Gravatar Thanks! Right info @ the right time.
# Posted By Mark Truluck | 10/11/07 7:20 AM
Igor Deruga's Gravatar I've been working on this problem in my code since yesterday :(
Very useful link, thank you!
# Posted By Igor Deruga | 10/11/07 8:14 PM
gonxalo's Gravatar thanks i was having the same problem, thanks a lot

gracias amigo
# Posted By gonxalo | 10/25/07 1:04 AM
Aryan's Gravatar Many thanks for this. It was tough for person like me to figureout where and what went wrong. You saved my time.
# Posted By Aryan | 12/6/07 9:27 PM
James's Gravatar Wow, amazing to see this is roadblock for a lot of people, me included. I hit this issue but luckily I moved on to other parts but probably wasted about two hours trying to figure out why it wasn't working. So glad you're on page 1 of google with this :-D
# Posted By James | 12/7/07 9:58 AM
Flo's Gravatar THANK YOU SO MUCH FOR THIS POST !
# Posted By Flo | 12/14/07 11:00 PM
Dale's Gravatar You rock! You just saved me many hours of debugging.
# Posted By Dale | 12/15/07 12:55 PM
OSteEL's Gravatar 1st time I find a bug solution in a few seconds... relevant topic ! merci bien ;)
# Posted By OSteEL | 1/21/08 2:40 PM
ranjit's Gravatar thanks--exactly the help I needed!
# Posted By ranjit | 1/24/08 3:44 AM
dhana's Gravatar Thanks a lot, I have the same problem and last 3 hours breaking my head. Very Helpful...
# Posted By dhana | 2/2/08 5:16 PM
Laco's Gravatar OMG! I will never catch theproblem....

Thx :)
# Posted By Laco | 2/14/08 3:36 PM
factorpi's Gravatar Thank you so much, you saved my life. :o)
# Posted By factorpi | 2/26/08 4:16 PM
Yoav's Gravatar thanks for making this easy to find. next time i'll go to google sooner.
# Posted By Yoav | 3/3/08 6:09 PM
christian's Gravatar oh my god!!!

i'm ready to cry. I thought I must be incredibly dumb. Thank you! Thank you! Thank you!
# Posted By christian | 5/22/08 6:55 AM
MadJay's Gravatar Heh! this little gem is still working hard! Just spent a couple hours trying to understand why my upload code works but my download blew chunks!
Refactoring broke my stuff!!

Many Thanks!
# Posted By MadJay | 6/18/08 8:59 PM
Minh's Gravatar Thanks so much! was scratching my head for a few days on this =]
# Posted By Minh | 6/20/08 2:58 PM
Remo's Gravatar Thanks Dude! Best info ever.
# Posted By Remo | 9/7/08 5:56 PM
pasi v's Gravatar i woke up 3 o'clock this morning after 5 hours of sleep just to think about why my download doesn't work. this made my day.

Kiitos
# Posted By pasi v | 9/11/08 7:57 AM
amazuzu's Gravatar Really Thanks!!! You've just saved some days of my time.
# Posted By amazuzu | 9/13/08 1:28 PM
Ariel Jakobovits's Gravatar Thank you.
# Posted By Ariel Jakobovits | 9/18/08 7:48 AM
guillaume's Gravatar Like all previous comment...!!! Thanks a lot...
# Posted By guillaume | 10/2/08 5:10 PM
Theo Lagendijk's Gravatar Wow! Thanks!
# Posted By Theo Lagendijk | 10/4/08 10:44 AM
Bernard khadra's Gravatar Hi ALL I you have a problem to download some files using FileReference.Download()
please check this example under the below link:
http://www.acoukess.info/bernard/download-in-AS3.z...
# Posted By Bernard khadra | 10/16/08 1:02 PM
Bernard khadra's Gravatar Hi ALL I you have a problem to download some files using FileReference.Download()
please check this example under the below link:
http://www.acoukess.info/bernard/download-in-AS3.z...
# Posted By Bernard khadra | 10/16/08 1:19 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.