0 Replies Latest reply on Dec 1, 2008 2:57 AM by Matt Gifford

    Mulitple File Downloads in AIR

    Matt Gifford Level 1
      ** EDIT **

      Just tested. I was throttling the downloads to 28kbps. Think there must be a timeout on the file functions, as when it was reset without throttling, the file downloaded 100%. Is there a way to reset/remove a timeout on a URLStream?

      **

      Good morning all.
      I'm having trouble with a file download component I've written. It worked fine debugging, but when testing an installed version of the application, there seems to be a problem with the loop, and files not completely downloading.

      I have tested, and one file in the loop is writing to the directory (event.complete action) at about 45%.
      How can I check if the total bytes downloaded is the same as total available, and if not, force it to carry on the download for that particular item?

      The code for the page is below:

      public function Update():void {
      if(appModel.fileArrCol.length > 0) {
      var urlReq:URLRequest = new URLRequest(appModel.fileArrCol[0][1].toString());
      trace(urlReq.url);
      urlStream = new URLStream();
      urlStream.addEventListener(IOErrorEvent.IO_ERROR, onStreamError);
      urlStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
      urlStream.addEventListener(Event.COMPLETE, fileTransferHandler);
      urlStream.load(urlReq);
      }

      }

      private function fileTransferHandler(event:Event):void {
      urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
      var fileLoc:String = appModel.fileArrCol[0][2].toString() + appModel.fileArrCol[0][0].toString();
      var file:File = File.applicationStorageDirectory.resolvePath(fileLoc);
      var fileStream:FileStream = new FileStream();
      fileStream.addEventListener(Event.CLOSE, fileClosed);
      fileStream.addEventListener(IOErrorEvent.IO_ERROR, onStreamError);
      fileStream.openAsync(file, FileMode.WRITE);
      fileStream.writeBytes(fileData, 0, fileData.length);
      fileStream.close();
      trace("File " + appModel.fileArrCol.length + ": " + appModel.fileArrCol[0][0].toString());
      }

      private function onStreamError(event:IOErrorEvent):void{
      //Alert.show("Invalid Username or Password!","Warning");
      this.showCloseButton =true;
      btnUpdate.visible = true;
      trace(event.text);
      }


      private function progressHandler(event:ProgressEvent):void {
      /* bar.setProgress( Number(event.bytesLoaded / event.bytesTotal), 1 ); */
      bar.setProgress(event.bytesLoaded,event.bytesTotal);
      progLabel.text = "Downloading " + Math.round(event.bytesLoaded / 1024) + " kb of " + Math.round(event.bytesTotal / 1024) + " kb " + (appModel.fileArr.length - 1) + " files remaining";
      }


      private function fileClosed(event:Event):void {
      // run a progress bar or sqlite table update here
      appModel.fileArrCol.removeItemAt(0);

      if (appModel.fileArrCol.length > 0){
      Update();
      } else {
      progLabel.text = "Uploads Complete";

      var thisServerVersion:String = appModel.fileDownloadServerVersion;
      var thisDescription:String = appModel.fileDownloadDescription;
      var thisDownloadDate:String = appModel.fileDownloadDate;

      // store the file download details within the database table
      if(appModel.checkFileDownloadHistory(thisServerVersion).valueOf() > 0) {
      // a record matches the version number in the xml. do nothing
      } else {
      appModel.storeFileDownloadHistory(thisServerVersion,thisDescription,thisDownloadDate);
      }
      // close the box down
      PopUpManager.removePopUp(this)
      }
      }

      public function init():void{
      btnUpdate.visible = false;
      this.showCloseButton = true;

      if (appModel.urlAvailable){
      Update();
      }else{
      Alert.show("No Network Connection Detected","Message");
      this.showCloseButton =true;
      btnUpdate.visible = true;
      }
      }


      public function titleWindow_close(evt:CloseEvent):void {
      PopUpManager.removePopUp(this)
      }

      The application reads the number of files from an xml file on the server. That works fine, but I think the loop is wrong. In debug, it traces the file names without a problem

      Please please please can someone help me? I need to get this sorted and resolved by this lunchtime.

      Many thanks in advance,

      Matt