Tech Blog

AS3/Flex FileReference download problem - solved

Posted At : July 16, 2007 4:08 PM 42 Comments

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

42 Comments

Anand 7/16/07 5:20 PM

Hmm good stuff dude. thanks for your info.

Richard T-J 7/16/07 5:30 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

Shaby 7/19/07 12:02 AM

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.

Andriy 7/20/07 3:56 PM

you have just saved my life :)

Shekar 8/1/07 10:09 PM

That saved me a lot of time and pain!!
Thanks to you and Google. Life is easier.:)

stef 8/13/07 5:08 AM

I HIT THIS and wasted like 3 hours! thank you!!! STEF

Chris 8/24/07 12:20 AM

Mark, you are my hero!!!

JeffO 8/25/07 7:12 AM

Thanks! Only wish I'd found your post sooner.

Mark Truluck 10/11/07 7:20 AM

Thanks! Right info @ the right time.

Igor Deruga 10/11/07 8:14 PM

I've been working on this problem in my code since yesterday :(
Very useful link, thank you!

gonxalo 10/25/07 1:04 AM

thanks i was having the same problem, thanks a lot

gracias amigo

Aryan 12/6/07 9:27 PM

Many thanks for this. It was tough for person like me to figureout where and what went wrong. You saved my time.

James 12/7/07 9:58 AM

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

Flo 12/14/07 11:00 PM

THANK YOU SO MUCH FOR THIS POST !

Dale 12/15/07 12:55 PM

You rock! You just saved me many hours of debugging.

OSteEL 1/21/08 2:40 PM

1st time I find a bug solution in a few seconds... relevant topic ! merci bien ;)

ranjit 1/24/08 3:44 AM

thanks--exactly the help I needed!

dhana 2/2/08 5:16 PM

Thanks a lot, I have the same problem and last 3 hours breaking my head. Very Helpful...

Laco 2/14/08 3:36 PM

OMG! I will never catch theproblem....

Thx :)

factorpi 2/26/08 4:16 PM

Thank you so much, you saved my life. :o)

Yoav 3/3/08 6:09 PM

thanks for making this easy to find. next time i'll go to google sooner.

christian 5/22/08 6:55 AM

oh my god!!!

i'm ready to cry. I thought I must be incredibly dumb. Thank you! Thank you! Thank you!

MadJay 6/18/08 8:59 PM

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!

Minh 6/20/08 2:58 PM

Thanks so much! was scratching my head for a few days on this =]

Remo 9/7/08 5:56 PM

Thanks Dude! Best info ever.

pasi v 9/11/08 7:57 AM

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

amazuzu 9/13/08 1:28 PM

Really Thanks!!! You've just saved some days of my time.

Ariel Jakobovits 9/18/08 7:48 AM

Thank you.

guillaume 10/2/08 5:10 PM

Like all previous comment...!!! Thanks a lot...

Theo Lagendijk 10/4/08 10:44 AM

Wow! Thanks!

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...

Bernard khadra 10/16/08 1:19 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...

Kuba 12/16/08 12:20 PM

Thanks a lot. It's saves me quite much time. GREAT JOB !!!

pat 1/26/09 5:48 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

cyberprodigy 2/8/09 3:19 PM

Yea.I was spending about an hour.
However it is totally wierd, because server is returning
206 Partial Content

Brr. Thanx, anyway.

Vinay Sharma 3/2/09 10:01 AM

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.

Rlyn 3/31/09 7:58 PM

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

panel 4/8/09 10:11 AM

Thanks 4 sharing. You saved my time also

Desperate as3 user 4/28/09 7:55 AM

Thanks a lot!!! is exacly what I need thank you very much

bootcat 5/10/09 5:33 PM

Thanks a lot . the same happened to me and the file doesnt get saved . You saved me a bunch.

Rez 6/22/09 5:53 PM

This is a winner. Thanks so much for posting it.

CarlMal 12/11/09 5:52 AM

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.