3 Replies Latest reply on Apr 27, 2009 5:38 AM by Barna Biro

    Function click() on button

    chandler124 Level 1



      I'm french (sorry for grammar mistakes ). I made a button but I want to use the function click() in order to : when you click on this button you will download a file but I don't know how to proceed.


      Here's my button :


          <mx:Button y="391" label="TŽlŽcharger" horizontalCenter="0" width="255" height="40" id="BtnTel" fontFamily="Verdana" color="#FFFFFF" fontSize="20" borderColor="#244D69" alpha="1.0" fillAlphas="[1.0, 1.0]" fillColors="[#072464, #084C69]" cornerRadius="20" click="finderredim.png"/>


      Can you tell me how to make this action please ?


      Thank you

        • 1. Re: Function click() on button
          Michael Borbor Level 4

          Hi there don't worry, I'm Ecuadorian so I think people over here is used to see grammatical errors. Look in the Help Files for FileReference, that's the class you'll use to upload and download files, and only works since Flash Player 10. Insted of doing what you wrote in your code when the click event if fire you'll do something like this


           public class DownloadFileExample extends Sprite
                  private var fileToDownload:FileReference;
                  public function DownloadFileExample()
                      var request:URLRequest = new URLRequest();
                      request.url = "http://www.[yourdomain].com/downloadfile.cfm";
                      request.method = URLRequestMethod.GET;
                      request.data = new URLVariables("id=2");
                      fileToDownload = new FileReference();
                          fileToDownload.download(request, "file2.txt");
                      catch (error:Error)
                          trace("Unable to download file.");

          And in your button click you'll call the function <mx:Button click="DownloadFileExample" />

          1 person found this helpful
          • 2. Re: Function click() on button
            Michael Borbor Level 4

            Please delete this double post.

            • 3. Re: Function click() on button
              Barna Biro Level 3

              Hi there,


              The correct way is to go with the FileReference as Michael already pointed out ( because it offers a lot of fun features and you can event keep track of the download progress ) but there would a second solution too. In case you feel that FileReference is way over your head, then you could simply use navigateToURL("yourFile.zip"); which would pop-up a download dialog ( just like when you right click and Save As a file from a HTML website ) where you can specify the location where to save the file.


              Again, navigateToURL is quite limited because of it's simplicity and FileReference should be used for more controlled downloading but it does not hurt to know most ( if not all ) your options.


              Best regards,

              Barna Biro

              1 person found this helpful