我到处寻找有关如何下载等的答案...
我个人更喜欢从 Greenshocks AS3 库中下载任何带有 LoaderMax 的文件(由于它是一个轻量级的加载器,它已经包含在我的大多数项目中。
我没有指定一个本地 URL,而是指定一个远程 URL .. lemme 向您展示一些代码:
public function downloadTxtJpgMp3():void
{
var queue:LoaderMax = new LoaderMax();
emptyLoader(); //this emptys the loader from previous attempts to load any files
queue.append( new DataLoader("http://www.70disco.com/lyrics/delegation_you_and_I.txt",{name:"test_txt" ,format:"text", estimatedBytes:4000}) );
queue.append( new ImageLoader( "http://4.bp.blogspot.com/-WQ3uAvGdPS0/UOB_OPS6rcI/AAAAAAAAKkU/HYaXXHVHTqc/s1600/whatever-dude-whatever.jpg" , {name:"test_img" , estimatedBytes:77000, container:this, alpha:0, scaleMode:"proportionalInside"}) );
queue.append( new MP3Loader( "http://nocturno.nsk.pt/otherpages/funny/mp3/2001-02/Cebola%20Mol/Cebola%20Mol%20-%20Satright%20No%20Chaser%20II.mp3" , {name:"test_mp3" , repeat:0, autoPlay: false}) );
queue.addEventListener(LoaderEvent.COMPLETE, onDownloadComplete);
queue.load();
}
下面是 COMPLETE 事件的处理程序。您还可以为您命名的 ERROR、FAIL、PROGRESS 等设置处理程序。
protected function onDownloadComplete(event:LoaderEvent):void
{
var objects:Array = event.currentTarget.content ;
var firstObjectILoaded: String //txt
var secondObjectILoaded:Bitmap //jpg
var thirdObjectILoaded: Sound //mp3
// ContentDisplay is found within greenshock
firstObjectILoaded = objects[0];
secondObjectILoaded = ((objects[1] as ContentDisplay).rawContent as Bitmap)
thirdObjectILoaded = objects[2];
trace(firstObjectILoaded);
thirdObjectILoaded.play();
}
请记住,LoaderMax 并不真正关心文件是本地文件还是远程文件,它只是将其加载到内存中。
从那时起,您可以决定是将其保存为文件还是仅使用它然后丢弃它。
如果要将其保存为文件,方法如下:(下面的 iOs 示例)
var str:String = File.applicationDirectory.nativePath;
//in iOs you can save on 4 different folder directories (according to apples rules)
// cache, temp, app support, and user documents (i don't cover this below)
appCache = new File(str +"/\.\./Library/Caches"); //cache folder (in which you put files that you dont care being erased. the iOs might delete those files in case of low memory
appTempData = new File(str +"/\.\./tmp"); //temp folder (just files you temporarily want to store
appData = new File(str +"/\.\./Library/Application\ Support"); //any file your application NEEDS in order to operate, this can't be deleted by the os
var fr:FileStream = new FileStream();
fr.open(
appTempData // appTempData or appCache or appData or userDocs
.resolvePath("myCache.txt"),FileMode.WRITE);
fr.writeUTFBytes("works");
fr.close();