all groups > flash actionscript > july 2007 >
You're in the

flash actionscript

group:

Flash/ASP.net File Upload Tool


Flash/ASP.net File Upload Tool schmidtjra
7/31/2007 9:56:17 PM
flash actionscript: Source Files - http://www.jmanproductions.com/Source.zip

Good afternoon,
I'm trying to recreate a Flash/ASP.net File Upload tool that I saw here:
http://www.codeproject.com/aspnet/FlashUpload.asp
This version was done with Flash 8, so I wanted to recreate it using Flash CS3
and AS3.
I was able to get the Flash 8 version working here:
http://www.jmanproductions.com/Default.aspx

So what's my problem? Well The version above uses parameters sent to the flash
player that the flash player uses for the upload method. Well I'm having
trouble pinpointing my problem with the following code. When I test it in
Visual Studio nothing happens I can't seem to figure out how to trace the value
of the parameters, which are also below the code. so if anyone could point me
in the right direction, or download the source, and help me out, I would much
appreciate it.

Thanks!

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=new 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 = new
URLRequest(root.loaderInfo.parameters.uploadPage);

public function Upload() {
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(280,50);
_statusLabel2.text="";
addChild(_statusLabel2);

_statusLabel3=new Label ;
_statusLabel3.move(300,50);
_statusLabel3.text="";
addChild(_statusLabel3);
}
private function configureListeners(dispatcher:IEventDispatcher):vo id {
dispatcher.addEventListener(Event.SELECT,selectHan dler);
dispatcher.addEventListener(Event.CANCEL,cancelHan dler);
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);
}
//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;
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, bytesLoaded:Number,
bytesTotal:Number):void {
_statusBar.mode="manual";
var temp:Number= bytesLoaded - _uploadedBytes2[event.target.name];
_uploadedBytes2[event.target.name]= bytesLoaded;
_uploadedBytes+= temp;
_statusBar.setProgress(_uploadedBytes,_totalBytes) ;
_statusLabel.text=GetSizeType(_uploadedBytes);
_statusLabel3.text=GetSizeType(_totalBytes);
}
private function completeHandler(event:Event):void {
_statusText.text += event.target.name + " uploaded.\n";
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
_filesCompleted ++;
_statusText.text += _filesCompleted + " of " + _totalFiles + " files
completed.\n";
_statusText.verticalScrollPosition=_statusText.max VerticalScrollPosition;
/*if (_filesCompleted == _totalFiles) {
FinishedUpload();
}*/
}
private function FinishedUpload():void {
Re: Flash/ASP.net File Upload Tool schmidtjra
8/1/2007 9:27:27 PM
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) ;
AddThis Social Bookmark Button