(转)FileReference保存读取文件

 package

{
 import flash.display.Loader;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.net.FileReference;
 import flash.utils.ByteArray;
 /**
  * 事实证明,fileRefence是可以打开本地文件的
  */
 public class TestFileRefrence extends Sprite
 {
  private var file:FileReference;
  public function TestFileRefrence()
  {
   this.stage.addEventListener(MouseEvent.CLICK,function():void
   {
    file=new FileReference();
    file.browse();
    file.addEventListener(Event.SELECT,selected);
   });
  }
  private function selected(e:Event):void
  {
   file.load();         //Flash Player 10, AIR 1.5
   file.addEventListener(Event.COMPLETE,on_loaded);
  }
  private function on_loaded(e:Event):void
  {
   var byteArray:ByteArray=ByteArray(e.target.data);                    //这里非常关键
   var loader:Loader=new Loader();
   loader.loadBytes(byteArray);
   this.addChild(loader);
  }
 }
}

 ========================保存文件======================================

var file:FileReference = new FileReference();
file.save(XML(XXXXX, "XXXX");

posted @ 2012-05-14 09:57  ☆A希亿  阅读(902)  评论(0编辑  收藏  举报