0 Replies Latest reply on Apr 15, 2009 2:11 PM by vuckovid

    How do you download a static file using a FileReference and URLRequest and show the progress?

      All,

       

         I'm trying to download static content (PDFs) off of my server using tutorials from adobe's site and some online as well and i'm having no luck showing an updated progress bar.  I attached the meat of my code below.  If a user clicks a hyperlink, it calls the downloadPDF method passing in a constant url defined.

       

        When debugging, i can see that the event.bytesLoaded is incrementing on each call to the progress method, however the bytesTotal is only set to the intitial number of bytes transferred on the first pass through the method.  (Ex:  First time the method is called, bytesLoaded and bytesTotal will be 4,000... which i dont understand... the bytesTotal should be the total size of the file being downloaded.. definitely not the case...  the next time the method is called, bytesLoaded will increases to some number while bytesTotal will remain at 4,000).  This happens during the entire file transfer.

       

      Am i missing something? Are the examples incorrect?  Also to note, i'm using sdk 3.2 (Not sure if that makes a difference.)

       

      Thanks for any response or insight.

       

      Damian

       

      <mx:Script>
              <![CDATA[
                  import com.ctc.fema.resources.Buttons;
                  import flash.net.FileReference;

       

                  private var fileRef:FileReference;
                  private var urlReq:URLRequest;

       

                  /**
                   * Constant values for user manual pdfs
                   */
                  private static const PDF_STRING:String = "blahblahblah.pdf";

       

                  private function init():void
                  {

       

                      /* Define file reference object and add a bunch of event listeners. */
                      fileRef = new FileReference();
                      fileRef.addEventListener(Event.COMPLETE, completeHandler);
                      fileRef.addEventListener(Event.OPEN, openHandler);
                      fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                  }

       

                  private function doEvent(evt:Event):void
                  {
                      /* Create shortcut to the FileReference object. */
                      var fr:FileReference = evt.currentTarget as FileReference;

       

                      try
                      {
                          /* Update the Model. */
                          fileRefModel.creationDate = fr.creationDate;
                          fileRefModel.creator = fr.creator;
                          fileRefModel.modificationDate = fr.modificationDate;
                          fileRefModel.name = fr.name;
                          fileRefModel.size = fr.size;
                          fileRefModel.type = fr.type;
                          /* Display the Text control. */
                      }
                      catch (err:*)
                      {
                          /* uh oh, an error of sorts. */
                      }
                  }

       

                  private function downloadPDF(url:String):void
                  {
                      /* Begin download. */
                      urlReq = new URLRequest(url);
                      fileRef.download(urlReq);
                  }

       

                  /**
                   * When the OPEN event has dispatched, change the progress bar's label
                   * and enable the "Cancel" button, which allows the user to abort the
                   * download operation.
                   */

       

                  private function openHandler(event:Event):void
                  {
                      downloadStatusContainer.visible = true;
                      downloadStatusContainer.includeInLayout = true;
                      downloadProgressBar.label = "DOWNLOADING %3%%";
                      cancelButton.enabled = true;
                  }

       

                  /**
                   * While the file is downloading, update the progress bar's status.
                   */
                  private function progressHandler(event:ProgressEvent):void
                  {
                      downloadProgressBar.setProgress(event.bytesLoaded, event.bytesTotal);
                  }

       

                  /**
                   * Once the download has completed, change the progress bar's label one
                   * last time and disable the "Cancel" button since the download is
                   * already completed.
                   */
                  private function completeHandler(event:Event):void
                  {
                      downloadStatusContainer.visible = false;
                      downloadStatusContainer.includeInLayout = false;
                      downloadProgressBar.label = "DOWNLOAD COMPLETE";
                      cancelButton.enabled = false;
                  }

       

                  /**
                   * Cancel the current file download.
                   */
                  public function cancelDownload():void
                  {
                      fileRef.cancel();
                      downloadProgressBar.label = "DOWNLOAD CANCELLED";
                      cancelButton.enabled = false;
                      downloadStatusContainer.visible = false;
                      downloadStatusContainer.includeInLayout = false;
                  }
              ]]>
          </mx:Script>