使用urlStream + fileStream下载多个文件

时间:2010-12-13 18:24:28

标签: arrays air download flex4

使用Flash Builder 4(使用4.1 flex)并找到/修改了一个脚本,该脚本在没有提示的情况下使用单个文件。现在我需要修改它以下载并保存多个文件。

代码的发起者说要引入一个数组,但不知道究竟该怎么做。 理想情况下,按下按钮一次,它启动该功能,然后该功能读取数组,并使用从数组的第一个结果定义的变量运行命令,然后更改为下一个。
这些值只会在不在本地或远程位置的文件名称上更改。

我知道如何在php中执行此操作但不是flex。有人能够完成这个吗?

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

        protected function download_clickHandler(event:MouseEvent):void
        {
            var remoteFile = "http://domain/01.jpg";
            var localFile = "C:/01.jpg";

            var urlStream = new URLStream();                
            var request = new URLRequest(remoteFile);                

            var fileStream = new FileStream();                

            var file = File.desktopDirectory.resolvePath(localFile);                

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

            var finishWriteFile = function()
            {
                if(urlStream.bytesAvailable > 0)
                {
                    var dataBuffer = new ByteArray();
                    urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                    fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                }                    

                fileStream.close();
                urlStream.close();

                return true;
            }                

            fileStream.openAsync(file, FileMode.WRITE);
            urlStream.load(request);                

            urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
            urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
            }
        }
    ]]>
</fx:Script>

<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" label="Update" id="download" click="download_clickHandler(event)" color="#FFFFFF" fontSize="30"/></s:WindowedApplication>

感谢您的任何意见!

1 个答案:

答案 0 :(得分:2)

这就是我现在拥有的,它的确有效。任何人都会看到它是如何完成的?

<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 download_clickHandler(event:MouseEvent):void
        {
            var filearray:Array = new Array();
            filearray[0]="pregame.jpg";
            filearray[1]="01.jpg";
            filearray[2]="02.jpg";
            filearray[3]="03.jpg";
            filearray[4]="04.jpg";
            filearray[5]="05.jpg";
            filearray[6]="06.jpg";
            filearray[7]="07.jpg";
            filearray[8]="08.jpg";
            filearray[9]="09.jpg";
            filearray[10]="10.jpg";
            for (var i:uint; i < filearray.length; i++) {
                var remoteURL = "http://www.domain.com/" + filearray[i];
                var localURL = "C:/dir/" + filearray[i];
                downloadFile(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);
            }
        }
    ]]>
</fx:Script>


<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" label="Update" id="download" click="download_clickHandler(event)" color="#FFFFFF" fontSize="30"/>