2010-09-01 64 views
2

我在網上發現了幾個腳本,並將它們結合在一起。 我想從網上下載文件到我的本地硬盤。 任何想法我做錯了什麼?AS3:URLStream將文件保存到桌面?

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對象沒有打開的流。但是,與網頁的連接熄滅。

任何想法? 謝謝你的幫助!

回答

5

我修改了你的代碼,這裏是一個可用的下載類。 (@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; 
     } 
    } 
} 
+0

@SébastienNussbaumer你應該已經給出了評語,讓帕斯卡爾知道這個 – 2013-08-06 07:40:10

+0

嗨,有人只是試圖修改關於「糾正錯誤的答案:waitingForDataToWrite總是假的,進展情況將不會派遣和下載的文件會完全回憶起來「但它可能被拒絕了(不是我)。隨時給我發送更新的代碼,以便我可以修改自己(並信任作者)pascal.dalfarra [at] gmail.com – Pascal 2013-08-06 08:25:25

+0

你的意思是[this one](http://stackoverflow.com/review/suggested-edits/2662320) )?它被接受了,但對我來說,聽起來非常嚴重,寧願與你討論而不直接批准它。如果是後續行動,不幸的是,被拒絕的建議幾乎無法找到,除了在審覈隊列中。這裏還有另外一個也是被@Sébastien之前被拒絕的:http://stackoverflow.com/review/suggested-edits/2662084 – 2013-08-06 08:28:32

0

這是一個遠射,因爲我從來沒有與FileStream(因爲你粘貼的錯誤消息說這個URLStream對象等)工作。但是,看來你FileStream對象(fs)沒有打開,你試圖1)寫入它,然後2)關閉它。我的理解是,如果文件沒有打開,任何這些操作都會引發錯誤。所以,也許值得這樣做。除此之外,你可以粘貼錯誤的堆棧跟蹤,所以它更清楚它起源於哪裏? (提示:如果您正在使用Flash IDE進行編譯,請在actionscript設置中檢查許可證調試;這將爲您提供更詳細的錯誤消息和行號;這對調試/故障排除有很大的幫助。