2012-02-07 87 views
1

我对Flex非常陌生。我想使用flex检索文件的二进制内容但失败。这是迄今为止代码:如何在flex中读取文件的二进制内容

// ActionScript file 

package source.fileApi{ 
    import flash.display.Sprite; 
    import flash.external.ExternalInterface; 
    import flash.system.Security; 
    import flash.utils.setTimeout; 

    import flash.net.FileReference; 
    import flash.net.FileFilter; 
    import flash.events.IOErrorEvent; 
    import flash.events.Event; 
    import flash.utils.ByteArray; 

    import mx.utils.URLUtil; 

    public class FileAPIMain { 
     private var debug:Boolean = false; 
     private var dataReaded:String; 
     private var fr:FileReference; 

     public function FileAPIMain():void{ 
      ExternalInterface.addCallback("setDebug", setDebug); 
      ExternalInterface.addCallback("onLoadFileClick", onLoadFileClick); 
      ExternalInterface.call("FileReader.__onFlashInitialized"); 
     } 

     public function log(message:String):void { 
      if (debug) { 
       ExternalInterface.call("FileReader.__log", encodeURIComponent("[FileReader] " + message)); 
      } 
     } 

     public function setDebug(val:Boolean):void { 
      debug = val; 
      if (val) { 
       log("debug enabled"); 
      } 
     } 

     public function onLoadFileClick():void{ 
      //create the FileReference instance 
      fr = new FileReference(); 

      //listen for when they select a file 
      fr.addEventListener(Event.SELECT, onFileSelect); 

      //listen for when then cancel out of the browse dialog 
      fr.addEventListener(Event.CANCEL,onCancel); 

      //open a native browse dialog that filters for text files 
      fr.browse(); 
     } 

     private function onFileSelect(e:Event):void{ 
      fr.removeEventListener(Event.SELECT, onFileSelect); 
      //listen for when the file has loaded 
      fr.addEventListener(Event.COMPLETE, onLoadComplete); 

      //listen for any errors reading the file 
      fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError); 

      //load the content of the file 
      fr.load(); 
     } 

     private function onCancel(e:Event):void{ 
      log("File Browse Canceled"); 
      fr = null; 
     } 

     private function onLoadComplete(e:Event):void{ 
     fr.removeEventListener(Event.COMPLETE, onLoadComplete); 

     //get the data from the file as a ByteArray 
     var data:ByteArray = fr.data as ByteArray; 

      dataReaded = data.readUTFBytes(data.bytesAvailable); 

      ExternalInterface.call("FileReader.__getSize", fr.size); 
      ExternalInterface.call("FileReader.__getName", fr.name); 
      ExternalInterface.call("FileReader.__getData", dataReaded); 
     ExternalInterface.call("FileReader.__takeAction"); 

      //clean up the FileReference instance 
      fr = null; 
     } 

     private function onLoadError(e:IOErrorEvent):void{ 
      log("Error loading file : " + e.text); 
     } 
    } 
} 

但这并没有给我的文件的二进制内容..可以在任何一个请告诉我如何检索使用Flex给定文件的完整的二进制内容(我使用FP 10.0).....

+0

使用调用FileReference.browse(纯AS3样品),并听取了完整的事件。然后,只需使用filereferernce.data中的readByte() – 2012-02-07 10:44:51

+0

@AdrianPirvulescu readByte()只返回一个字节,但我需要完整的二进制内容,请您在答案部分提供一些编码... – Tareq 2012-02-07 11:37:56

回答

0

如果您使用FLashPlayer 10+,然后使用FileReference。

见下

 

var buttonShape:Shape = new Shape(); 

buttonShape.graphics.beginFill(0x336699); 
buttonShape.graphics.drawCircle(50, 50, 25); 

var button = new SimpleButton(buttonShape, buttonShape, buttonShape, buttonShape); 

addChild(button); 

var fileRef:FileReference= new FileReference(); 
button.addEventListener(MouseEvent.CLICK, onButtonClick); 

function onButtonClick(e:MouseEvent):void 
{ 
    fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]); 
    fileRef.addEventListener(Event.SELECT, onFileSelected); 
} 

function onFileSelected(e:Event):void 
{ 
    fileRef.addEventListener(Event.COMPLETE, onFileLoaded); 
    fileRef.load(); 
} 

function onFileLoaded(e:Event):void 
{ 
    // here we get the data 
    var theData:Object = e.target.data; 

    // next you can manipulate the data as you like! ;) depending on the type 
} 

+0

@AdrianPivulescu生成上面的代码像ByteArray这样的对象。但我需要在二进制模式下的文件内容。请帮忙... – Tareq 2012-02-10 07:20:51