等待数组函数完成:

时间:2010-12-14 21:49:10

标签: arrays air flex4 event-listener

在Flex 4.1中使用Flash Builder 4。 我有一个空中应用程序,可以下载数组中指定的文件。 我试图在文件写完磁盘后改变状态。

在文件下载完成之前,将调用函数finalscreen。这是我用来测试它的大型视频文件所显而易见的。

<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>

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我没有找到在数组中执行的函数之间“暂停”的方法。

相反,我考虑过使用文件计数器,所以加载它们的顺序并不重要,只要加载了所有11个加载最终状态。

这是我的代码供其他人参考:

<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"/>