1 Reply Latest reply on Jul 6, 2010 10:18 PM by aktell2007

    File Download Script problems and changes if possible ....

    aktell2007 Level 1

      Hi there,

       

      I have started to work with a 'Download' file I have found, and which is based on the ADOBE 'Up and Downloading Files' HELP example.

      http://manewc.com/2008/08/28/downloading-files-with-flex-3/

       

      The code works on the site mentioned above very well !

       

      But when I run it on my computer with the original code (Http-Url-Link) or with my specified own (Http-Url-Link) of my Server (1mB or 4mB to Download) the 'Progress bar' including the 'Downloading %' is 'NOT' showing progress !!!

       

      Has anybody had found these problems as well using this code???

       

      Now ignoring these faults at the moment I have modified the code to address two different files for download (.zip and .rar) to choose from. Now I would like to return and active a timer in my Mxml App. code from the AS3 script once the computer Operating Systems 'SAVE AS' button is clicked.

       

      Unfortunately nothing I tried to code so far has worked. So please if anybody has an idea it would be much appriciated !!! Or even steer me towards where I could find other Flex 3 / AS3 code to download files from a Server.

       

      regards aktell2007

        • 1. Re: File Download Script problems and changes if possible ....
          aktell2007 Level 1

          Hi there again,

           

          Have to re-address the problem as I still are not able to make this code work properly !!!

           

          Here is the full code: Please if somebody could help - I have tried everything I know without success, and I can't get an answer from the author either.

           

          The Original Application File:

           

          <mx:Application 
               xmlns:mx="http://www.adobe.com/2006/mxml" 
               xmlns:example="com.components.example.programmingas3.fileio.*" 
               xmlns="*" 
          
                    horizontalAlign="center">
          
                    <example:FileDownload 
                         id="fileDownload" 
               creationComplete="fileDownload.init(downloadProgress, cancelDownload, 'http://www.yoursite.com/file.rar');"/>
                    
                    <mx:HBox>
                    <mx:Panel title="Download File"
                          paddingTop="10" 
                             paddingBottom="10" 
                          paddingLeft="10" 
                          paddingRight="10">
          
                    <mx:ProgressBar id="downloadProgress" label=""/>
          
                    <mx:ControlBar horizontalAlign="right">
                    
                         <mx:Button label="Download…"
                              id="startDownload"  
                                   click="fileDownload.startDownload();"/>
          
                         <mx:Button label="Cancel"
                              id="cancelDownload"
                                   click="fileDownload.cancelDownload();" 
                                        enabled="false"/>
          
                    </mx:ControlBar>
                    </mx:Panel>
                    </mx:HBox>
          
          </mx:Application>
          

           

          The Original AS file:

           

          package com.components.example.programmingas3.fileio {
          
                   import flash.events.*;
                   import flash.net.FileReference;
                   import flash.net.URLRequest;
          
                   import mx.controls.Button;
                   import mx.controls.ProgressBar;
                   import mx.core.UIComponent;
          
                   public class FileDownload extends UIComponent {
          
                  public var DOWNLOAD_URL:String;
          
                  private var fr:FileReference;
          
                    // Define reference to the download ProgressBar component.
                  private var pb:ProgressBar;
          
                 // Define reference to the "Cancel" button which will immediately stop the download in progress.
                  private var btn:Button;
          
                  public function FileDownload() {
          
                  }
          
                  /**
                   * Set references to the components, and add listeners for the OPEN,
                   * PROGRESS, and COMPLETE events.
                   */
                  public function init(pb:ProgressBar, btn:Button, dURL:String):void {
                    // Set up the references to the progress bar and cancel button, which are passed from the calling script.
                      this.pb = pb;
                      this.btn = btn;
          
                      DOWNLOAD_URL = dURL;
          
                      fr = new FileReference();
                      fr.addEventListener(Event.OPEN, openHandler);
                      fr.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                      fr.addEventListener(Event.COMPLETE, completeHandler);
                  }
          
                  /**
                   * Immediately cancel the download in progress and disable the cancel button.
                   */
                  public function cancelDownload():void {
                      fr.cancel();
                      pb.label = "DOWNLOAD CANCELLED";
                      btn.enabled = false;
                  }
          
                  /**
                   * Begin downloading the file specified in the DOWNLOAD_URL constant.
                   */
                  public function startDownload():void {
                      var request:URLRequest = new URLRequest();
                      request.url = DOWNLOAD_URL;
                      fr.download(request);
                  }
          
                  /**
                   * 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 {
                      pb.label = "DOWNLOADING %3%%";
                      btn.enabled = true;
                  }
          
                  /**
                   * While the file is downloading, update the progress bar's status and label.
                   */
                  private function progressHandler(event:ProgressEvent):void {
                      pb.setProgress(event.bytesLoaded, event.bytesTotal);
                  }
          
                  /**
                   * Once the download has completed, change the progress bar's label and
                   * disable the "Cancel" button since the download is already completed.
                   */
                  private function completeHandler(event:Event):void {
                      pb.label = "DOWNLOAD COMPLETE";
                      pb.setProgress(0, 100);
                      btn.enabled = false;
                  }
              }
          }