/ Published in: ActionScript 3
Here's a simple example of how a user can download their file to their desktop
Expand |
Embed | Plain Text
private var request:URLRequest; private var localRef:FileReference; protected function buttonClick():void { request = new URLRequest("soundbyte.mp3"); localRef = new FileReference(); try { // Prompt and downlod file localRef.download( request ); } catch (error:Error) { trace("Unable to download file."); } }
Comments
Subscribe to comments
You need to login to post a comment.

Thank you for posting the code above. I tried using it on my first as3 site (yes, total noob here) and got two errors: "1013: The private attribute may be used only on class property definitions" and 1150: The protected attribute can only be used on class property definitions.
hey flanoob04. In Flash CS4, if you're using the ActionScript window, you need to remove the access specifiers (aka "private", "protected", "public", etc).
thanks for your help chrisaiv. i tried doing what you suggested, but still didn't get it to work. in a nut shell, i'd like to allow users to download a .pdf resume from my site by clicking on a button symbol. i removed access specifiers, as you advised, and changed the file name to match my local file name, but to no avail. Shouldn't I be adding a button event listener somewhere? i appreciate any help you could provide. thank you.
You most certainly need an Event Listener + Handler and well... this Snippet is only a fraction of what you need.
Hopefully this will get you started: button_mc.addEventListener( MouseEvent.CLICK, onMouseClickHandler, false, 0, true ); function onMouseClickHandler( e:MouseEvent ):void { buttonClick(); }
I suggest you visit another Snippet code of mine that hopefully will explain how to set Event Listeners + Handlers. http://snipplr.com/view/5022/as3-ondragover-ondragout-onreleaseoutside/
thanks for your help chrisaiv. i tried doing what you suggested, but still didn't get it to work. in a nut shell, i'd like to allow users to download a .pdf resume from my site by clicking on a button symbol. i removed access specifiers, as you advised, and changed the file name to match my local file name, but to no avail. Shouldn't I be adding a button event listener somewhere? i appreciate any help you could provide. thank you.
thanks chrisaiv. i was able to figure out a simpler way to accomplish the task. i'm posting it here for the benefit of whomever is struggling with a similar problem:
download_btn.addEventListener(MouseEvent.CLICK, downloadFile);
function downloadFile (evtObj:Event):void {
var Location:URLRequest = new URLRequest ("http://www.yourdomain.com/yourfilename.pdf");
navigateToURL (Location, "_tab");
}
Great work flanoob04!
Just to be clear, in your example, you are not really downloading a PDF using Flash. You are calling a PDF file and relying on the browser to complete the remaining action. In Firefox, the PDF may download but in Safair, the PDF will probably display.
Thanks! :)