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
Hmm good stuff dude. thanks for your info.
# Posted By Anand | 7/16/07 5:20 PM
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
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
you have just saved my life :)
# Posted By Andriy | 7/20/07 3:56 PM
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
I HIT THIS and wasted like 3 hours! thank you!!! STEF
# Posted By stef | 8/13/07 5:08 AM
Mark, you are my hero!!!
# Posted By Chris | 8/24/07 12:20 AM
Thanks! Only wish I'd found your post sooner.
# Posted By JeffO | 8/25/07 7:12 AM
Thanks! Right info @ the right time.
# Posted By Mark Truluck | 10/11/07 7:20 AM
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
thanks i was having the same problem, thanks a lot

gracias amigo
# Posted By gonxalo | 10/25/07 1:04 AM
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
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
THANK YOU SO MUCH FOR THIS POST !
# Posted By Flo | 12/14/07 11:00 PM
You rock! You just saved me many hours of debugging.
# Posted By Dale | 12/15/07 12:55 PM
1st time I find a bug solution in a few seconds... relevant topic ! merci bien ;)
# Posted By OSteEL | 1/21/08 2:40 PM
thanks--exactly the help I needed!
# Posted By ranjit | 1/24/08 3:44 AM
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
OMG! I will never catch theproblem....

Thx :)
# Posted By Laco | 2/14/08 3:36 PM
Thank you so much, you saved my life. :o)
# Posted By factorpi | 2/26/08 4:16 PM
thanks for making this easy to find. next time i'll go to google sooner.
# Posted By Yoav | 3/3/08 6:09 PM
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
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
Thanks so much! was scratching my head for a few days on this =]
# Posted By Minh | 6/20/08 2:58 PM
Thanks Dude! Best info ever.
# Posted By Remo | 9/7/08 5:56 PM
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
Really Thanks!!! You've just saved some days of my time.
# Posted By amazuzu | 9/13/08 1:28 PM
Thank you.
# Posted By Ariel Jakobovits | 9/18/08 7:48 AM
Like all previous comment...!!! Thanks a lot...
# Posted By guillaume | 10/2/08 5:10 PM
Wow! Thanks!
# Posted By Theo Lagendijk | 10/4/08 10:44 AM
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
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
Thanks a lot. It's saves me quite much time. GREAT JOB !!!
# Posted By Kuba | 12/16/08 12:20 PM
Thanks a lot. I had the same problem. The example in the documentation is incorrect. I would have eventually found it, but this saved me a lot of time
# Posted By pat | 1/26/09 5:48 PM
Yea.I was spending about an hour.
However it is totally wierd, because server is returning
206 Partial Content

Brr. Thanx, anyway.
# Posted By cyberprodigy | 2/8/09 3:19 PM
var fileRef:FileReference = new FileReference();
function downloadFile():void {
fileRef.download(new URLRequest("myFile.txt"), "myFile.txt");
}
Solved partially.
If in windows "Hide known file extensions" is checked.
the save as popup wiill come with filename as "myFile".
if I download it it will work fine and file be with ".txt" extension automatically.
But
If I rename "myFile" to "myFile11" it will be downloaded
but become unuseable.
Have any workaround for this.
# Posted By Vinay Sharma | 3/2/09 10:01 AM
Hi,
I was wondering if its possible to download from an XML list. I tried referencing from xml file's url node, but it seems it won't detect, I kept getting an error saying I can only put http or https address/protocol.

Thanks,
Reina Lyn Ben
# Posted By Rlyn | 3/31/09 7:58 PM
Thanks 4 sharing. You saved my time also
# Posted By panel | 4/8/09 10:11 AM
Thanks a lot!!! is exacly what I need thank you very much
# Posted By Desperate as3 user | 4/28/09 7:55 AM
Thanks a lot . the same happened to me and the file doesnt get saved . You saved me a bunch.
# Posted By bootcat | 5/10/09 5:33 PM
This is a winner. Thanks so much for posting it.
# Posted By Rez | 6/22/09 5:53 PM
I spent 4 hours trying to call a php scrift from Flash before I abandoned that approach and figured I'd try to do it from Flash and spent another hour trying to get,what seemed so simple in the examples, to work. Finally, I found your brilliant bit of stuff bud. Thanks for that You're my hero.
# Posted By CarlMal | 12/11/09 5:52 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.