1  
     8   
     9  import com.sekati.core.CoreObject;
    10  import com.sekati.events.Event;
    11  import com.sekati.events.Dispatcher;
    12  import com.sekati.utils.Delegate;
    13  
    14  import flash.net.FileReference;
    15  
    16  
    20  class com.sekati.net.File extends CoreObject {
    21  
    22  	private var _this:File;
    23  	private var _ref:FileReference;
    24  	private var _listener:Object;
    25  	public var _imageType:Object = {description: "Image Files", extension: "*.jpg;*.gif;*.png"};
    26  	public var _textType:Object = {description:"Text Files", extension:"*.txt; *.rtf"};
    27  	public var _docTypes:Object = {description: "Documents", extension: "*.pdf; *.doc; *.xls"};
    28  	public var _webTypes:Object = {description: "Web Files", extension: "*.html; *.htm; *.xhtml; *.php; *.asp; *.aspx; *.cfm; *.xml; *.xsl; *.xslt; *.css; *.js; *.jsp"};
    29  	public var _flashTypes:Object = {description: "Flash Files", extension: "*.swf; *.fla; *.as; *.flp; *.flv"};
    30  	public var _audioTypes:Object = {description: "Audio Files", extension:"*.mp3, *.aiff, *.wav"};
    31  	public var _videoTypes:Object = {description: "Video Files", extension: "*.mpg; *.mpeg; *.mp4; *.mov; *.qt; *.avi; *.wmv; *.asf"};
    32  	public var _anyType:Object = {description: "All Files", extension:"*.*"};
    33  	public var _allTypes:Array = [ _imageType, _textType, _docTypes, _webTypes, _flashTypes, _audioTypes, _videoTypes, _anyType ];
    34  	
    35  	public var onSelectEVENT:String = "FILE_onSelect";
    36  	public var onCancelEVENT:String = "FILE_onCancel";
    37  	public var onOpenEVENT:String = "FILE_onOpen";
    38  	public var onProgressEVENT:String = "FILE_onProgress";
    39  	public var onCompleteEVENT:String = "FILE_onComplete";
    40  	public var onHTTPErrorEVENT:String = "FILE_onHTTPError";
    41  	public var onIOErrorEVENT:String = "FILE_onIOError";
    42  	public var onSecurityErrorEVENT:String = "FILE_onSecurityError";
    43  
    44  	
    47  	public function File() {
    48  		super( );
    49  		_this = this;
    50  		_listener = new Object( );
    51  		_ref = new FileReference( );
    52  		_ref.addListener( _listener );
    53  		_listener.onSelect = Delegate.create( _this, onSelect );
    54  		_listener.onCancel = Delegate.create( _this, onCancel );
    55  		_listener.onOpen = Delegate.create( _this, onOpen );
    56  		_listener.onProgress = Delegate.create( _this, onProgress );
    57  		_listener.onComplete = Delegate.create( _this, onComplete );
    58  		_listener.onHTTPError = Delegate.create( _this, onHTTPError );
    59  		_listener.onIOError = Delegate.create( _this, onIOError );
    60  		_listener.onSecurityError = Delegate.create( _this, onSecurityError );
    61  	}
    62  
    63  	
    69  	public function download(url:String, defaultFileName:String):Boolean {
    70  		return _ref.download( url, defaultFileName );
    71  	}
    72  
    73  	
    78  	public function browse(type:Array):Boolean {
    79  		if (!type) type = _allTypes;
    80  		return _ref.browse( type );	
    81  	}
    82  
    83  	
    92  	public function upload(url:String, uploadDataFieldName:String, testUpload:Boolean):Boolean {
    93  		if (!uploadDataFieldName) uploadDataFieldName = "Filedata";
    94  		if (testUpload == undefined) testUpload = false;
    95  		return _ref.upload( url, uploadDataFieldName, testUpload );
    96  	}
    97  
    98  	
    99  	
   100  	
   104  	public function get creationDate():Date {
   105  		return _ref.creationDate;	
   106  	}
   107  
   108  	
   112  	public function get creator():String {
   113  		return _ref.creator;	
   114  	}
   115  
   116  	
   120  	public function get modificationDate():Date {
   121  		return _ref.modificationDate;	
   122  	}
   123  
   124  	
   128  	public function get name():String {
   129  		return _ref.name;	
   130  	}
   131  
   132  	
   136  	public function get size():Number {
   137  		return _ref.size;	
   138  	}
   139  
   140  	
   144  	public function get type():String {
   145  		return _ref.type;	
   146  	}
   147  
   148  	
   149  	private function onSelect(file:FileReference):Void {
   150  		trace( "onSelect: " + file.name );
   151  		Dispatcher( new Event( onSelectEVENT, _this, {file: file} ) );
   152  		
   157  	}
   158  
   159  	private function onCancel(file:FileReference):Void {
   160  		trace( "onCancel" );
   161  		Dispatcher( new Event( onCancelEVENT, _this, {file: file} ) );
   162  	}
   163  
   164  	private function onOpen(file:FileReference):Void {
   165  		trace( "onOpen: " + file.name );
   166  		Dispatcher( new Event( onOpenEVENT, _this, {name: file.name} ) );
   167  	}
   168  
   169  	private function onProgress(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
   170  		trace( "onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal );
   171  		Dispatcher( new Event( onProgressEVENT, _this, {bytesLoaded: bytesLoaded, bytesTotal: bytesTotal} ) );
   172  	}
   173  
   174  	private function onComplete(file:FileReference):Void {
   175  		trace( "onComplete: " + file.name );
   176  		Dispatcher( new Event( onCompleteEVENT, _this, {file:file} ) );
   177  	}
   178  
   179  	private function onHTTPError(file:FileReference):Void {
   180  		trace( "onHTTPError: " + file.name );
   181  		Dispatcher( new Event( onHTTPErrorEVENT, _this, {name: file.name} ) );
   182  	}
   183  
   184  	private function onIOError(file:FileReference):Void {
   185  		trace( "onIOError: " + file.name );
   186  		Dispatcher( new Event( onIOErrorEVENT, _this, {name: file.name} ) );
   187  	}	
   188  
   189  	private function onSecurityError(file:FileReference, errorString:String):Void {
   190  		trace( "onSecurityError: " + file.name + " errorString: " + errorString );
   191  		Dispatcher( new Event( onSecurityErrorEVENT, _this, {name: file.name, errorString: errorString} ) );
   192  	}
   193  }