下载文件到本地

public function downLoad(sourceurl:String,targetfile:String):void{
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    loader.addEventListener(Event.COMPLETE,
     function(e:Event):void
     {
      var airf:File =File.applicationStorageDirectory.resolvePath(targetfile);
      trace(airf.nativePath);
      //trace(airf.url);
      var fs:FileStream = new FileStream();
      fs.open(airf, FileMode.WRITE);
      fs.writeBytes(loader.data);
      fs.close();
      airf.downloaded = true;          
      //打开此下载完成的文件
      airf.openWithDefaultApplication();
     });
    loader.addEventListener(ProgressEvent.PROGRESS,function(e:ProgressEvent):void{
     trace(e.bytesLoaded/e.bytesTotal+"/"+100);
    });
    var req:URLRequest = new URLRequest(sourceurl);
    loader.load(req);
   }
posted @ 2012-06-15 14:28  糖豆爸爸  阅读(190)  评论(0编辑  收藏  举报
Live2D