开发者

Waiting until array function finishes to:

Using Flash Builder 4 with flex 4.1. I have an air app that downloads files that are specified in an array. I am trying to change state after the files have finished being written to the disk.

The function finalscreen is being called before the files are finished downloading. This is evident with the large video file that I put on to test it.

<fx:Script>
    <![CDATA[
        function finalscreen()
        {
            this.currentState="Finished";
        }      
        private function initiate_download(event:MouseEvent):void
        {
            this.currentState="Working";
            var filearray:Array = new Array();
            filearray[0]="0.jpg";
            filearray[1]="1.jpg";
            filearray[2]="2.jpg";
            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]="1.avi";
            for (var i:uint; i < filearray.length; i++) {
                var remoteURL = "http://domain/dir/" + filearray[i];
                var localURL = "C:/dir/" + filearray[i];
                downloadFile(remoteURL, localURL);
            }
    finalscreen();

            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.open(file, FileMode.WRITE);
                urlStream.load(request);

                // Add event listeners
               开发者_如何学编程 urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
            }
   }
     ]]>
</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>

Thanks!


Ok I did not find a way to "pause" between the function being executed in the array.

Instead I thought about using a counter of files, so it didn't matter what order they were loaded, as long as all 11 loaded then load the final state.

Here is my code for others reference:

<fx:Script>
    <![CDATA[
        private function close_window(event:MouseEvent):void 
        {
            this.nativeApplication.exit();
        }
        private function finalScreen():void
        {
            this.currentState="Finished";
        }
        private function initiate_download(event:MouseEvent)
        {
            var filearray:Array = new Array();
            filearray[0]="0.jpg";
            filearray[1]="1.exe";
            filearray[2]="02.jpg";
            filearray[3]="2.exe";
            filearray[4]="04.jpg";
            filearray[5]="05.jpg";
            filearray[6]="4.exe";
            filearray[7]="07.jpg";
            filearray[8]="08.jpg";
            filearray[9]="09.jpg";
            filearray[10]="3.exe";
            for(var i:uint; i < filearray.length; i++) 
            {
                var remoteURL = "http://domain/dir/" + filearray[i];
                var localURL = "C:/dir/" + filearray[i];
                downloadFile(remoteURL, localURL);
            }

            var filenumber = 0;

            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.UPDATE);
                urlStream.load(request);

                // Add event listeners
                urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
                fileStream.addEventListener(Event.CLOSE, updateProgress);

                function updateProgress(event:Event):void
                {
                    filenumber = filenumber+1;
                    progbar.setProgress(filenumber*10, 110);
                    if(10*filenumber == 110)
                    {
                        finalScreen();
                    }
                }
            }

        }
    ]]>
</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"/>
<mx:ProgressBar includeIn="Working" id="progbar" mode="manual" x="121" y="107" fontSize="18" textAlign="center"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜