Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.97 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to read binary content of a file in flex
  2. // ActionScript file
  3.  
  4. package source.fileApi{
  5.     import flash.display.Sprite;
  6.     import flash.external.ExternalInterface;
  7.     import flash.system.Security;
  8.     import flash.utils.setTimeout;
  9.  
  10.     import flash.net.FileReference;
  11.     import flash.net.FileFilter;
  12.     import flash.events.IOErrorEvent;
  13.     import flash.events.Event;
  14.     import flash.utils.ByteArray;
  15.  
  16.     import mx.utils.URLUtil;
  17.  
  18.     public class FileAPIMain {
  19.         private var debug:Boolean = false;
  20.         private var dataReaded:String;
  21.         private var fr:FileReference;
  22.  
  23.         public function FileAPIMain():void{
  24.             ExternalInterface.addCallback("setDebug", setDebug);
  25.             ExternalInterface.addCallback("onLoadFileClick", onLoadFileClick);
  26.             ExternalInterface.call("FileReader.__onFlashInitialized");
  27.         }
  28.  
  29.         public function log(message:String):void {
  30.             if (debug) {
  31.                 ExternalInterface.call("FileReader.__log", encodeURIComponent("[FileReader] " + message));
  32.             }
  33.         }
  34.  
  35.         public function setDebug(val:Boolean):void {
  36.             debug = val;
  37.             if (val) {
  38.                 log("debug enabled");
  39.             }
  40.         }
  41.  
  42.         public function onLoadFileClick():void{
  43.             //create the FileReference instance
  44.             fr = new FileReference();
  45.  
  46.             //listen for when they select a file
  47.             fr.addEventListener(Event.SELECT, onFileSelect);
  48.  
  49.             //listen for when then cancel out of the browse dialog
  50.             fr.addEventListener(Event.CANCEL,onCancel);
  51.  
  52.             //open a native browse dialog that filters for text files
  53.             fr.browse();
  54.         }
  55.  
  56.         private function onFileSelect(e:Event):void{
  57.             fr.removeEventListener(Event.SELECT, onFileSelect);
  58.             //listen for when the file has loaded
  59.             fr.addEventListener(Event.COMPLETE, onLoadComplete);
  60.  
  61.             //listen for any errors reading the file
  62.             fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
  63.  
  64.             //load the content of the file
  65.             fr.load();
  66.         }
  67.  
  68.         private function onCancel(e:Event):void{
  69.             log("File Browse Canceled");
  70.             fr = null;
  71.         }
  72.  
  73.       private function onLoadComplete(e:Event):void{
  74.         fr.removeEventListener(Event.COMPLETE, onLoadComplete);
  75.  
  76.         //get the data from the file as a ByteArray
  77.         var data:ByteArray = fr.data as ByteArray;
  78.  
  79.             dataReaded = data.readUTFBytes(data.bytesAvailable);
  80.  
  81.             ExternalInterface.call("FileReader.__getSize", fr.size);
  82.             ExternalInterface.call("FileReader.__getName", fr.name);
  83.             ExternalInterface.call("FileReader.__getData", dataReaded);
  84.       ExternalInterface.call("FileReader.__takeAction");
  85.  
  86.             //clean up the FileReference instance
  87.             fr = null;
  88.         }
  89.  
  90.         private function onLoadError(e:IOErrorEvent):void{
  91.             log("Error loading file : " + e.text);
  92.         }
  93.    }
  94. }