Finally. I had a co-worker of mine help me debug the problem. Apparently my
problem was not with the parameter begin brought in, because once I looked on
the server, I noticed my files had indeed uploaded. So I turned to the event
handlers. I had mistakenly assigned all the listeners to the FileRefList
object. My co-worker pointed out that the Cancel and Select ones get assigned
to the FileRefList object, and the rest get assigned to the FileRef object in
the upload function. I have pasted my latest code. Also, for all you want to
try it out, you can download the source here:
http://www.jmanproductions.com/Source.zip My next step will be to figure out where to add the remove listeners. My
ultimate goal will be for Flash to show the file or files in a data grid once
they are uploaded. If they are images, they will show up as thumbnails.
Thanks,
J-man
Actionscript:
package {
import flash.display.MovieClip;
import fl.controls.Button;
import fl.controls.ProgressBar;
import fl.controls.TextArea;
import fl.controls.UIScrollBar;
import fl.controls.Label;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.events.*;
public class Upload extends MovieClip {
private var _browseBtn:Button;
private var _uploadBtn:Button;
private var _statusText:TextArea;
private var _statusBar:ProgressBar;
private var _statusLabel:Label;
private var _statusLabel2:Label;
private var _statusLabel3:Label;
private var _fileRefList:FileReferenceList;
private var _uploadedBytes:Number=0;
private var _uploadedBytes2:Array;
private var _totalBytes:Number=0;
private var _totalFiles:Number=0;
private var _filesCompleted:Number=0;
private var _uploadPage:URLRequest;
public function Upload() {
_fileRefList =new FileReferenceList()
_uploadPage = new URLRequest(root.loaderInfo.parameters.uploadPage);
init();
}
private function init():void {
createTextArea();
createProgressBar();
createButtons();
createLabels();
configureListeners(_fileRefList);
_browseBtn.addEventListener(MouseEvent.CLICK,brows eFiles);
_uploadBtn.addEventListener(MouseEvent.CLICK,uploa dFiles);
}
//Init Functions
private function createTextArea():void {
_statusText=new TextArea();
_statusText.text= ""
_statusText.move(5,5);
_statusText.setSize(200,60);
addChild(_statusText);
}
private function createProgressBar():void {
_statusBar=new ProgressBar ;
_statusBar.move(220,40);
_statusBar.visible = false;
addChild(_statusBar);
}
private function createButtons():void {
_browseBtn=new Button ;
_browseBtn.move(220,5);
_browseBtn.width=60;
_browseBtn.label="Browse";
addChild(_browseBtn);
_uploadBtn=new Button ;
_uploadBtn.move(310,5);
_uploadBtn.width=60;
_uploadBtn.label="Upload";
_uploadBtn.enabled=false;
addChild(_uploadBtn);
}
private function createLabels():void {
_statusLabel=new Label ;
_statusLabel.move(220,50);
_statusLabel.text="";
addChild(_statusLabel);
_statusLabel2=new Label ;
_statusLabel2.move(271,50);
_statusLabel2.text="";
addChild(_statusLabel2);
_statusLabel3=new Label ;
_statusLabel3.move(285,50);
_statusLabel3.text="";
addChild(_statusLabel3);
}
private function configureListeners(dispatcher:IEventDispatcher):vo id {
dispatcher.addEventListener(Event.SELECT,selectHan dler);
dispatcher.addEventListener(Event.CANCEL,cancelHan dler);
}
private function addFileEventListeners(dispatcher:IEventDispatcher) :void {
dispatcher.addEventListener(Event.OPEN,openHandler );
dispatcher.addEventListener(ProgressEvent.PROGRESS ,progressHandler);
dispatcher.addEventListener(Event.COMPLETE,complet eHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(SecurityErrorEvent.SEC URITY_ERROR,
securityErrorHandler);
}
/*private function removeFileEventListeners(dispatcher:IEventDispatch er):void
{
dispatcher.removeEventListener(Event.OPEN,openHand ler);
dispatcher.removeEventListener(ProgressEvent.PROGR ESS,progressHandler);
dispatcher.removeEventListener(Event.COMPLETE,comp leteHandler);
dispatcher.removeEventListener(IOErrorEvent.IO_ERR OR, ioErrorHandler);
dispatcher.removeEventListener(SecurityErrorEvent. SECURITY_ERROR,
securityErrorHandler);
}*/
//Button Handlers
private function browseFiles(event:Event):void {
_fileRefList.browse();
}
private function uploadFiles(event:Event):void {
var list:Array=_fileRefList.fileList;
var fileRef:FileReference;
if (_uploadBtn.label == "Upload") {
_browseBtn.enabled=false;
_uploadBtn.label="Cancel";
_statusText.text ="Starting Upload:" + '\n';
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
_totalFiles=list.length;
_filesCompleted=0;
_uploadedBytes=0;
_uploadedBytes2=[];
_statusLabel2.text="of";
for (var i:Number=0; i < list.length; i++) {
fileRef=list[i];
_statusText.text+= "Attempting to upload " + fileRef.name + '\n';
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
_uploadedBytes2[fileRef.name]=0;
addFileEventListeners(fileRef);
fileRef.upload(_uploadPage);
}
} else {
_statusLabel.text="";
_statusLabel2.text="";
_statusLabel3.text="";
_statusText.text+= "Upload Cancelled." + '\n';
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
for (var j:Number=0; j < list.length; j++) {
fileRef=list[j];
fileRef.cancel();
}
_uploadBtn.label="Upload";
_browseBtn.enabled=true;
}
}
//Event Handlers
private function selectHandler(event:Event):void {
_uploadBtn.enabled = true;
_statusBar.visible = true;
_statusText.text = "Files selected to upload: \n";
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
var list:Array=_fileRefList.fileList;
var fileRef:FileReference;
_totalBytes=0;
for (var i:int=0; i < list.length; i++) {
fileRef=list[i];
_totalBytes+= fileRef.size;
_statusText.text+= fileRef.name + '\n';
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
}
}
private function cancelHandler(event:Event):void {
_uploadBtn.enabled=false;
_statusText.text="No files selected. \n";
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
}
private function openHandler(event:Event):void {
_statusText.text+= "Uploading " + event.target.name + " please wait...\n";
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
}
private function progressHandler(event:ProgressEvent):void {
_statusBar.mode="manual";
var temp:Number= event.bytesLoaded - _uploadedBytes2[event.target.name];
_uploadedBytes2[event.target.name]= event.bytesLoaded;
_uploadedBytes+= temp;
_statusBar.setProgress(_uploadedBytes,_totalBytes) ;