Posted By

chrisaiv on 03/09/09


Tagged

as3


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

themill
mattlohmann
johnwun
Pingpongbal
TrentSkunk
tspitzr
retsub79
Josueriv


AS3: Download a File to the Desktop


 / Published in: ActionScript 3
 

Here's a simple example of how a user can download their file to their desktop

  1. private var request:URLRequest;
  2. private var localRef:FileReference;
  3.  
  4. protected function buttonClick():void
  5. {
  6. request = new URLRequest("soundbyte.mp3");
  7. localRef = new FileReference();
  8.  
  9. try
  10. {
  11. // Prompt and downlod file
  12. localRef.download( request );
  13. }
  14. catch (error:Error)
  15. {
  16. trace("Unable to download file.");
  17. }
  18. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: flanoob04 on October 19, 2009

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.

Posted By: chrisaiv on October 20, 2009

hey flanoob04. In Flash CS4, if you're using the ActionScript window, you need to remove the access specifiers (aka "private", "protected", "public", etc).

Posted By: flanoob04 on October 20, 2009

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.

Posted By: chrisaiv on October 21, 2009

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/

Posted By: flanoob04 on October 21, 2009

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.

Posted By: flanoob04 on October 21, 2009

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");
}

Posted By: chrisaiv on October 31, 2009

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.

Posted By: RubenBristian on April 8, 2011

Thanks! :)

You need to login to post a comment.