我一直在寻找一个完整的答案,以实现多个下载的MP3文件downloadManager的web和stackoverflow。我已经阅读了大量的文章,并意识到最高效的设施是使用ByteArray,URLStream和FileStream来节省t盘。我执行如下:Adobe Air多国下载
<?xml version="1.0" encoding="utf-8"?>
<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"
creationComplete="downloadFiles()">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.filesystem.*;
import flash.net.URLStream;
public var stream:URLStream;
public var fileData:ByteArray;
public var fileStream:FileStream;
public var file:File;
public var trackString:String;
public var filearray:Array;
public var urlReq:URLRequest;
private function downloadFiles():void
{
filearray = new Array();
filearray[0]="Crazy";
filearray[1]="Distant Lover";
filearray[2]="Easy Going";
filearray[3]="Give In To Love";
filearray[4]="MuzicAgeErotic Moods";
filearray[5]="MuzicAgeGive In To Love";
filearray[6]="MuzicAgePieces Of Dreams";
filearray[7]="MuzicAgeThere will never be another you";
filearray[8]="Remembered";
filearray[9]="Teach Me Tonight";
downloadNextFile();
}
private function downloadNextFile():void{
trackString = filearray.shift();
urlReq = new URLRequest("http://localhost:8080/multiDownloadSampleApp7- debug/MuzicAge/"+trackString+".mp3");
stream = new URLStream();
file = File.documentsDirectory.resolvePath("C:/Users/development/test/"+trackString+".mp3");
fileStream = new FileStream();
stream.addEventListener(ProgressEvent.PROGRESS, progressBar);
stream.addEventListener(Event.COMPLETE, writeComplete);
stream.load(urlReq);
}
private function writeComplete(evt:Event):void
{
fileData = new ByteArray();
stream.readBytes(fileData,0,stream.bytesAvailable);
fileStream.openAsync(file, FileMode.WRITE)
fileStream.writeBytes(fileData,0,fileData.length);
fileStream.close();
stream.removeEventListener(ProgressEvent.PROGRESS, progressBar);
stream.removeEventListener(Event.COMPLETE, writeComplete);
downloadNextFile();
}
private function progressBar(evt:ProgressEvent):void
{
// progressBar
}
]]>
</fx:Script>
</s:WindowedApplication>
虽然它似乎捕获并保存所有的MP3的磁盘,我不断收到以下错误信息。
错误#2044:未处理的IOErrorEvent :.文本=错误#2032:流错误。在multiDownloadSampleApp7/writeComplete()[C:\ Users \ development \ newgageProjects \ multiDownloadSampleApp7 \ src \ multiDownloadSampleApp7.mxml:multiDownloadSampleApp7/downloadNextFile()[C:\ Users \ development \ newgageProjects \ multiDownloadSampleApp7 \ src \ multiDownloadSampleApp7.mxml:40] 59] [卸载SWF] multiDownloadSampleApp7.swf
当我关闭错误并查看保存目录时,我找到了我在数组中请求的所有MP3。
Cadin,谢谢添加一个测试数组是否为空的函数完美工作。非常感谢!!!!! – user1045857