FileReference.download() in SWF file fails

This stung me last night and I searched a fair bit to find out why. Kinda obvious I guess.

I am posting a link to the Adobe Technote and pasting a bit of it straight here.

From Adobe Technote

To allow a FileReference instance to accept a response, it needs to be defined in a persistent scope. This requires that the instance be defined either within the timeline of your movie or as a property of your class and not within the local scope of a function call.

Will not work

function downloadFile():void {
var fileRef:FileReference = new FileReference();
fileRef.download(new URLRequest("myFile.txt"), "myFile.txt");
}
This will
var fileRef:FileReference = new FileReference();
function downloadFile():void {
fileRef.download(new URLRequest("myFile.txt"), "myFile.txt");
}

Comments
Critter's Gravatar This kicked my butt most of the evening. Couldn't figure out why for the life of me my code wasn't working. Ended up doing this huge, ugly mess to download a file. stumbled on your post and was able to cut it all back to two lines again.

Thanks for blogging it.
# Posted By Critter | 1/12/08 2:39 PM