Clase Download - ActionScript3 | Phoxer.com
Clase Download - ActionScript3
03/02/2008

Muchas veces necesitamos manejar bajadas de archivos en nuestros proyectos, esta clase las programé para ahorrar tiempo a la hora de manejar archivos.

La clase DownloadFile la implementamos de esta manera:

import phoxer.Files.DownloadFile;
 
var dw:DownloadFile= new DownloadFile();
 
dw.backProgress=function(num:Number):void{
	trace(String("Downloading: "+num+"%"));
};
 
dw.backCancel=function():void{
	trace("Canceled");
};
 
dw.backComplete=function(file:FileReference):void{
	trace(file.name+" Downloaded");
};
 
dw.backError=function():void{
	trace("Error");
};
 
//Start Download
dw.Download(fileToDownload);
 
//Cancel
dw.cancelDownload();


acá esta la clase DownloadFile:

/**
DownloadManager By .:[PHOXER]:.
http://www.phoxer.com
v 2.8;
*/
package phoxer.Files{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	public class DownloadFile{
		private var FileRef:FileReference;
		private var fileToDownload:URLRequest;
		//funciones
		public var backComplete:Function=null;
		public var backCancel:Function=null;
		public var backProgress:Function=null;
		public var backError:Function=null;
		public var backSelected:Function=null;
		public function Download(url:String):void{
			fileToDownload = new URLRequest();
			fileToDownload.url = url;
			FileRef = new FileReference();
			FileRef.download(fileToDownload);
			//-
			DWHandlers(FileRef);
		}
		public function cancelDownload():void {
			FileRef.cancel();
			if(backCancel!=null){
				backCancel();
			}
		}
		private function DWHandlers(ds:IEventDispatcher):void {
			if(backComplete!=null){
				ds.addEventListener(Event.COMPLETE, EventComplete,false,0,true);
			}
			if(backCancel!=null){
				ds.addEventListener(Event.CANCEL, EventCancelar,false,0,true);
			}
			if(backSelected!=null){
				ds.addEventListener(Event.SELECT, EventSelected,false,0,true);
			}
			if(backError!=null){
				ds.addEventListener(IOErrorEvent.IO_ERROR, EventErrores,false,0,true);
			}
			if(backProgress!=null){
				ds.addEventListener(ProgressEvent.PROGRESS, EventProgress,false,0,true);
			}
		}
		private function EventSelected(e:Event):void{
			backSelected();
		}
		private function EventCancelar(e:Event):void {
			backCancel();
		}
		private function EventErrores(e:IOErrorEvent):void {
			backError();
		}
		private function EventComplete(e:Event):void {
			backComplete(FileRef);
		}
		private function EventProgress(e:ProgressEvent):void {
				var total:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
				backProgress(total);
		}
	}
}