0 Replies Latest reply on Aug 9, 2007 11:00 AM by johnsonj26

    Download Files from Server to Client

    johnsonj26 Level 1
      I have a flex application that sends data to a php page that then generates a pdf file and returns the name of that file to the application. All of that works fine. The problems start when I try to then download that file to the client.

      This is the Flex client code:

      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var pdfFiles:ArrayCollection;

      private function requestPDFHandler (event:ResultEvent):void {
      if( event.result.Files == null ) { /* Table is empty */
      /*Do Nothing*/
      }else if( event.result.Files.File is ObjectProxy ) { /* Table has only one record */
      pdfFiles = new ArrayCollection( [event.result.Files.File] );
      }else { /* Table has many records */
      pdfFiles = event.result.Files.File as ArrayCollection;
      }

      downloadPDF();
      }

      private function getPDF():void {
      requestPDF.send();
      }

      import flash.display.Sprite;
      import flash.events.*;
      import flash.net.FileReference;
      import flash.net.URLRequest;
      import flash.net.URLRequestMethod;

      private function downloadPDF():void {
      //trace (pdfFiles.list.getItemAt(0).FileName);
      var DOWNLOAD_URL:String = " http://" + dbLoc + "/PDF/tmp/" + pdfFiles.list.getItemAt(0).FileName;
      //trace(DOWNLOAD_URL);
      var fr:FileReference = new FileReference();
      var request:URLRequest = new URLRequest();
      request.url = DOWNLOAD_URL;
      //configureListeners(fr);
      fr.addEventListener(Event.CANCEL, cancelHandler);
      fr.download(request,"Product List.pdf");
      }

      private function cancelHandler(event:Event):void {
      trace("cancelHandler: " + event);
      }

      ]]>
      </mx:Script>

      <mx:HTTPService id="requestPDF" url=" http://{dbLoc}/PDF/MakePDFDoc.php" method="POST" result="requestPDFHandler(event)">
      <mx:request xmlns="">
      <ProdGroupID>{cboProdGroup.selectedItem.ID}</ProdGroupID><CurrencyID>{cboCurrency.selecte dItem.ID}</CurrencyID>
      </mx:request>
      </mx:HTTPService>

      <mx:Button x="574" y="804" label="Save as PDF" width="144" height="34" fontFamily="Verdana" fontSize="12" cornerRadius="12" id="btnSavePDF" enabled="true" fillColors="[#33332d, #33332d]" fillAlphas="[1.0, 1.0]" color="#ffffff" textRollOverColor="#808080" textSelectedColor="#808080" borderColor="#000000" mouseUp="getPDF()"/>


      When the button is clicked it sends to the php page fine and recieves the name of the file. When it gets to "fr.download(request,"Product List.pdf");" in the downloadPDF function, the 'fr:FileReference' object gives me this error - "Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.". I don't understand what is causing the error. Any suggestions?