2 Replies Latest reply on Dec 17, 2010 11:10 AM by mubhcaeb78

    Completion of the function with array and fileStream write

    mubhcaeb78

      Hello,

      I am trying to get a state change after all the files are downloaded.

      I am using fileStream to automatically update files.  I wish to avoid prompting.

       

       

      How can I get the list of files downloaded, and after the last one, trigger the next state to show completed?

      The EventListener I have in there now will not work.  Putting a this.state after closing the file stream of the last doesn't work.

      Here is the code I currently have.  I put various 50mb videos in the array to test the leaving filestream going after function completed.

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                                  xmlns:s="library://ns.adobe.com/flex/spark"
                                  xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="200"
                                  title="Updater"
                                  showStatusBar="false">
           <fx:Style source="Main.css"/>
           <fx:Script>
                <![CDATA[
                     private function close_window(event:MouseEvent):void
                     {
                          this.nativeApplication.exit();
                     }
                    
                     private function initiate_download(event:MouseEvent):void
                     {
                          var filearray:Array = new Array();
                          filearray[0]="0.jpg";
                          filearray[1]="2.avi";
                          filearray[2]="3.avi";
                          filearray[3]="3.jpg";
                          filearray[4]="4.jpg";
                          filearray[5]="5.jpg";
                          filearray[6]="6.jpg";
                          filearray[7]="7.jpg";
                          filearray[8]="8.jpg";
                          filearray[9]="9.jpg";
                          filearray[10]="10.jpg";
                          filearray[11]="4.avi";
                          for(var i:uint; i < 11; i++)
                          {
                               var remoteURL = "http://domain/files/" + filearray[i];
                               var localURL = "C:/files/" + filearray[i];
                               downloadFile(remoteURL, localURL);
                          }
                          var remoteURL = "http://domain/files/" + filearray[11];
                          var localURL = "C:/files/" + filearray[11];
                          downloadlastFile(remoteURL, localURL);
                         
                          function downloadFile(url, filename)
                          {
                               // Create the stream for the data request
                               var urlStream = new URLStream();
                              
                               // Used to initiate request for remote file
                               var request = new URLRequest(url);
                              
                               // Create file stream
                               var fileStream = new FileStream();
                              
                               // Create a reference to a location on disk
                               var file = File.desktopDirectory.resolvePath(filename);
                              
                               // Called as download progresses
                               var writeFile = function()
                               {
                                    // Write to file
                                    if (urlStream.bytesAvailable > 51200)
                                    {
                                         var dataBuffer = new ByteArray();
                                         urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                                         fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                                    }
                                    return true;
                               }
                              
                               // Called when download completes
                               var finishWriteFile = function()
                               {
                                    // Write to file
                                    if(urlStream.bytesAvailable > 0)
                                    {
                                         var dataBuffer = new ByteArray();
                                         urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                                         fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                                    }
                                   
                                    // Close streams
                                    fileStream.close();
                                    urlStream.close();
                                    return true;
                               }
                              
                               // Initiate download
                               fileStream.openAsync(file, FileMode.WRITE);
                               urlStream.load(request);
                              
                               // Add event listeners
                               urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                               urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
                          }
                         
                          function downloadlastFile(url, filename)
                          {
                               // Create the stream for the data request
                               var urlStream = new URLStream();
                              
                               // Used to initiate request for remote file
                               var request = new URLRequest(url);
                              
                               // Create file stream
                               var fileStream = new FileStream();
                              
                               // Create a reference to a location on disk
                               var file = File.desktopDirectory.resolvePath(filename);
                              
                               // Called as download progresses
                               var writeFile = function()
                               {
                                    // Write to file
                                    if (urlStream.bytesAvailable > 51200)
                                    {
                                         var dataBuffer = new ByteArray();
                                         urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                                         fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                                    }
                                    return true;
                               }
                              
                               // Called when download completes
                               var finishWriteFile = function()
                               {
                                    // Write to file
                                    if(urlStream.bytesAvailable > 0)
                                    {
                                         var dataBuffer = new ByteArray();
                                         urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                                         fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                                    }
                                   
                                    // Close streams
                                    fileStream.close();
                                    urlStream.close();
                                    return true;
                               }
                              
                               // Initiate download
                               fileStream.openAsync(file, FileMode.WRITE);
                               urlStream.load(request);
                              
                               // Add event listeners
                               urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                               urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
                               fileStream.addEventListener(Event.COMPLETE, finishstate);

                              
                               var finishstate = function()
                               {
                                    this.setcurrentState="Finished";
                               }
                              
                          }
                     }
                ]]>
           </fx:Script>
          
           <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
           </fx:Declarations>
          
           <s:states>
                <s:State name="Start"/>
                <s:State name="Working"/>
                <s:State name="Finished"/>
           </s:states>
          
           <mx:Image x="0" y="0" source="background.jpg"/>
           <s:Button x="50" y="110" includeIn="Start" label="Update" id="download" click="currentState='Working'; initiate_download(event)" color="#FFFFFF" fontSize="30" width="300" height="40"/>
           <s:Button x="50" y="110" includeIn="Finished" label="Close Window" id="closer" click="close_window(event)" color="#FFFFFF" fontSize="30" width="300" height="40"/>    
           <s:RichText includeIn="Start" x="-22" y="85" text="Press Update to begin." width="444" textAlign="center" fontSize="14" id="starttext" color="#FFFFFF"/>
           <s:RichText includeIn="Working" x="-22" y="85" text="Downloading and updating files...." width="444" textAlign="center" fontSize="14" id="workingtext" color="#848484"/>
           <s:RichText includeIn="Finished" x="109" y="85" text="Update completed." width="191" textAlign="center" fontSize="14" id="finishedtext" color="#49D30C"/>
      </s:WindowedApplication>

       

      Also as a sidenote, does anyone know how to get a progressbar going on various files like that?  I would imagine doing it based on the loop, since each file will be roughly the same size.. there won't be picture/movies intertwined.