actionscript-3 - AS3 : URLStream saving files to desktop?

标签 actionscript-3 air

我在网上找到了一些脚本并将它们组合到此。 我想将文件从网络下载到本地硬盘。 知道我做错了什么吗?

var fs:FileStream;
var stream:URLStream;
var _output:Boolean = false;

init();
startDownload('http://www.teachenglishinasia.net/files/u2/purple_lotus_flower.jpg');

function init() { 
    stream = new URLStream();
    stream.addEventListener(ProgressEvent.PROGRESS, _dlProgressHandler); 
    stream.addEventListener(Event.COMPLETE, _dlCompleteHandler);
    stream.addEventListener(Event.OPEN, _dlStartHandler);
    fs = new FileStream();
    fs.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, _writeProgressHandler)
}

function startDownload(url:String):void {
     //fs.openAsync(lfile, FileMode.APPEND);
     _output = false;
     stream.load(new URLRequest(url));
}

function downloadComplete():void {
     var fileData:ByteArray = new ByteArray();
     stream.readBytes(fileData,0,stream.bytesAvailable);
     fs.writeBytes(fileData,0,fileData.length);
     fs.close(); 
}

function writeToDisk():void {
     _output = false;
     var fileData:ByteArray = new ByteArray();
     stream.readBytes(fileData,0,stream.bytesAvailable);
     fs.writeBytes(fileData,0,fileData.length);
}

function _dlProgressHandler(evt:ProgressEvent):void {
     if(_output){
         writeToDisk();   
     }
}

function _dlCompleteHandler(evt:Event):void { 
    downloadComplete();
} 

function _dlStartHandler(evt:Event):void {
     _output = true; 
}

function _writeProgressHandler(evt:OutputProgressEvent):void{
     _output = true;
}

Flash 不断告诉我:错误:错误 #2029:此 URLStream 对象没有打开流。但是与网页的连接断开了。

有什么想法吗? 感谢您的帮助!

最佳答案

我修改了您的代码,这是一个有效的下载程序类。 (@SébastienNussbaumer 改进了这个答案,@TobiasKienzler 审核了更改:非常感谢大家!)

使用简单:

var downLoader:Downloader = new Downloader();
downLoader.addEventListener(DownloadEvent.DOWNLOAD_COMPLETE, function(event:DownloadEvent):void{
    trace("Download complete: ");
    trace("\t"+event.url);
    trace("->\t"+event.file.url);
});
var file:File = File.applicationStorageDirectory.resolvePath("downloaded.mp3");
downLoader.download("http://dl.dropbox.com/u/18041784/Music/Lana%20Del%20Rey%20-%20Born%20To%20die%20%28Gemini%20Remix%29.mp3", file);

下载完成时的输出:

Download complete: 
    http://dl.dropbox.com/u/18041784/Music/Lana%20Del%20Rey%20-%20Born%20To%20die%20%28Gemini%20Remix%29.mp3
->  app-storage:/downloaded.mp3

享受:-)

package com.tatstyappz.net
{
    import flash.events.DataEvent;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.OutputProgressEvent;
    import flash.events.ProgressEvent;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.net.URLRequest;
    import flash.net.URLStream;
    import flash.utils.ByteArray;

    public class Downloader extends EventDispatcher
    {
        [Event(name="DownloadComplete", type="com.tatstyappz.net.DownloadEvent")]

        private var file:File;
        private var fileStream:FileStream;
        private var url:String;
        private var urlStream:URLStream;

        private var waitingForDataToWrite:Boolean = false;

        public function Downloader()
        {
            urlStream = new URLStream();

            urlStream.addEventListener(Event.OPEN, onOpenEvent);
            urlStream.addEventListener(ProgressEvent.PROGRESS, onProgressEvent); 
            urlStream.addEventListener(Event.COMPLETE, onCompleteEvent);

            fileStream = new FileStream();
            fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, writeProgressHandler)

        }

        public function download(formUrl:String, toFile:File):void {
            this.url = formUrl;
            this.file = toFile;
            fileStream.openAsync(file, FileMode.WRITE);
            urlStream.load(new URLRequest(url));
        }

        private function onOpenEvent(event:Event):void {
            waitingForDataToWrite = true;

            dispatchEvent(event.clone());
        }

        private function onProgressEvent(event:ProgressEvent):void {
            if(waitingForDataToWrite){
                writeToDisk();
                dispatchEvent(event.clone());
            }
        }

        private function writeToDisk():void {
            var fileData:ByteArray = new ByteArray();
            urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
            fileStream.writeBytes(fileData,0,fileData.length);
            waitingForDataToWrite = false;

            dispatchEvent(new DataEvent(DataEvent.DATA));
        }

        private function writeProgressHandler(evt:OutputProgressEvent):void{
            waitingForDataToWrite = true;
        }

        private function onCompleteEvent(event:Event):void {
            if(urlStream.bytesAvailable>0)
                writeToDisk();
            fileStream.close();

            fileStream.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, writeProgressHandler);

            dispatchEvent(event.clone());
            // dispatch additional DownloadEvent
            dispatchEvent(new DownloadEvent(DownloadEvent.DOWNLOAD_COMPLETE, url, file));
        }

    }
}

以及事件类:

package com.tatstyappz.net
{
    import flash.events.Event;
    import flash.filesystem.File;

    public class DownloadEvent extends Event
    {
        public static const DOWNLOAD_COMPLETE:String = "DownloadComplete";

        public var url:String;
        public var file:File;

        public function DownloadEvent(type:String, url:String, file:File)
        {
            super(type, true);
            this.url = url;
            this.file = file;
        }

        override public function toString():String{
            return super.toString() + ": "+ url + " -> "+file.url;
        }
    }
}

关于actionscript-3 - AS3 : URLStream saving files to desktop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3614199/

相关文章:

ios - 如何在 Windows 上调试 iPad Flash 应用程序?

actionscript-3 - 构建小型 GUI 引擎 : visible vs. addChild/removeChild

flash - 如何只渲染一个 BitmapFilter 到 BMD?

android - netstream 无法在 adobe air - amazon s3 - signed cloudfront 上运行

javascript - 安全的 adobe air html/javascript 代码

windows - 在 AIR 应用程序中捕获系统信息

actionscript-3 - 闪存 : Listen to all events of a type with one eventlistener

javascript - 如何将 sharethis 按钮添加到我的 Flash 文件中?

actionscript-3 - AS3.0 播放声音无延迟

actionscript-3 - ActionScript USB麦克风口吃