Flash file uploader with progress bar







ActionScript Code:





/********************************

Developed by Anil kumar N D

anilkumarnd@gmail.com

Flash Professional CS5

ActionScript 3.0

14 February 2011

*********************************/




package {

import flash.display.MovieClip;

import flash.net.FileReference;

import flash.events.*;

import flash.net.URLRequest;

import flash.net.URLVariables;




public class upload extends MovieClip {




private var _file:FileReference;





/*******************************************************************************

Default constructor

********************************************************************************/

public function upload() {

this.addEventListener(Event.ENTER_FRAME,fn_enterFrameHandler);

}




/*******************************************************************************

This function is a ENTER_FRAME event handler of "this"

input

IN_Event : Event

********************************************************************************/

private function fn_enterFrameHandler(IN_Event:Event):void{

if(this.loaderInfo.bytesTotal == this.loaderInfo.bytesLoaded){

this.removeEventListener(Event.ENTER_FRAME,fn_enterFrameHandler);

_file = new FileReference;

_file.addEventListener(Event.CANCEL, fn_cancelHandler);

_file.addEventListener(Event.COMPLETE, fn_completeHandler);

_file.addEventListener(HTTPStatusEvent.HTTP_STATUS, fn_httpStatusHandler);

_file.addEventListener(IOErrorEvent.IO_ERROR, fn_ioErrorHandler);

_file.addEventListener(Event.OPEN, fn_openHandler);

_file.addEventListener(ProgressEvent.PROGRESS, fn_progressHandler);

_file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fn_securityErrorHandler);

_file.addEventListener(Event.SELECT, fn_selectHandler);

_file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fn_uploadCompleteDataHandler);

btn_browse.addEventListener(MouseEvent.CLICK,fn_browserClickHandler);

btn_upload.addEventListener(MouseEvent.CLICK,fn_uploadClickHandler);

btn_cancel.addEventListener(MouseEvent.CLICK,fn_cancelClickHandler);

mc_progressBar.mc_bar.scaleX = 0;


}


}




/*******************************************************************************

This function is a click event handler of btn_browse

input

IN_Event : MouseEvent

********************************************************************************/

private function fn_cancelClickHandler(IN_Event:MouseEvent):void{

_file.cancel();

mc_progressBar.mc_bar.scaleX = 0;

btn_upload.enabled = false;

btn_browse.enabled = true;

btn_cancel.enabled = false;

txt_server.text = "File upload cancelled";

}




/*******************************************************************************

This function is a click event handler of btn_browse

input

IN_Event : MouseEvent

********************************************************************************/

private function fn_uploadClickHandler(IN_Event:MouseEvent):void{

btn_upload.enabled = false;

btn_browse.enabled = false;

btn_cancel.enabled = true;

_file.upload(new URLRequest("fileUpload.php"));


}




/*******************************************************************************

This function is a click event handler of btn_browse

input

IN_Event : MouseEvent

********************************************************************************/

private function fn_browserClickHandler(IN_Event:MouseEvent):void{

_file.browse();

txt_server.text = "Select your file";


}




/*******************************************************************************

These functions are the event handlers of _file

********************************************************************************/

private function fn_cancelHandler(IN_Event:Event):void {

trace("cancelHandler: " + IN_Event);

}

private function fn_completeHandler(IN_Event:Event):void {

trace("completeHandler: " + IN_Event);

btn_cancel.enabled = false;

btn_browse.enabled = true;

mc_progressBar.mc_bar.scaleX = 0;

}

private function fn_uploadCompleteDataHandler(IN_Event:DataEvent):void {

trace("uploadCompleteData: " + IN_Event);

var variables:URLVariables = new URLVariables(IN_Event.data);

txt_server.text = variables.dataFromServer;

}

private function fn_httpStatusHandler(IN_Event:HTTPStatusEvent):void {

trace("httpStatusHandler: " + IN_Event);

}

private function fn_ioErrorHandler(IN_Event:IOErrorEvent):void {

trace("ioErrorHandler: " + IN_Event);

}

private function fn_openHandler(IN_Event:Event):void {

trace("openHandler: " + IN_Event);

}

private function fn_progressHandler(IN_Event:ProgressEvent):void {

trace("progressHandler name=" + _file.name + " bytesLoaded=" + IN_Event.bytesLoaded + " bytesTotal=" + IN_Event.bytesTotal);

mc_progressBar.mc_bar.scaleX = IN_Event.bytesLoaded/IN_Event.bytesTotal;

txt_server.text = "Uploading "+ int((IN_Event.bytesLoaded/IN_Event.bytesTotal)*100) + "%";
}

private function fn_securityErrorHandler(IN_Event:SecurityErrorEvent):void {

trace("securityErrorHandler: " + IN_Event);

}

private function fn_selectHandler(IN_Event:Event):void {

txt_filename.text = _file.name;

btn_upload.enabled = true;

txt_server.text = "You have selected '"+_file.name+"'";


}



}

}




Comments

Popular posts from this blog

Simple Flash Analog Clock - AS3

AS3 - Access objects from external SWF files

Flash CS3 - XML Guestbook (AS3)