views:

1173

answers:

3

I have a Flex3 application which has to be capable of uploading multiple files and monitoring each files individual progress using a label NOT a progress bar.

My problem is that a generic progress handler for the uploads has no way (that I know of) of indicating WHICH upload it is that is progressing. I know that a file name is available to check but in the case of this app the file name might be the same for multiple uploads.

My question: With a generic progress handler how does one differentiate between 2 multiple uploads with the same file name?

EDIT: answerers may assume that I am a total newb to Flex... because I am.

+1  A: 

If you are listening for ProgressEvents, these events have a currentTarget attribute that would have a reference to the object that has registered the event listener.

I'm assuming you know which file-uploading object goes with each object in the first place.

EDIT: Example using FileReference:

import flash.net.FileReference;
import flash.events.ProgressEvent;
import flash.utils.Dictionary;

public var files:Dictionary = new Dictionary();  // This will hold all the FileReference objects

public function loadFile(id:String):void
{
 var file:FileReference = new FileReference();

 // Listen for the progress event on this FileReference... will call the same function for every progress event
 file.addEventListener(ProgressEvent.PROGRESS, onProgress);

 // TODO: listen for errors and actually upload a file, etc.

 // Add file to the dictionary (as key), with value set to an object containing the id
 files[file] = { 'id': id };
}

public function onProgress(event:ProgressEvent):void
{
 // Determine which FileReference dispatched thi progress event:
 var file:FileReference = FileReference(event.target);

 // Get the ID of the FileReference which dispatched this function:
 var id:String = files[file].id;

 // Determine the current progress for this file (in percent):
 var progress:Number = event.bytesLoaded / event.bytesTotal;

 trace('File "' + id + '" is ' + progress + '% done uploading');
}


// Load some files:
loadFile('the first file');
loadFile('the second file');
Cameron
I thought that the object that HAS to register the progress event is the file reference? OK say I have 2 objects - the FileReference and the object i can use to track the files. How do i attach the progressEvent to the tracking object so that it gets fired on the upload of the file reference? Or am i thinking about this the wrong way?
Darko Z
I'll edit my answer to have an example
Cameron
+1  A: 

I use this:

  private function _addFileListeners(dispatcher:IEventDispatcher):void {
      dispatcher.addEventListener(Event.OPEN, this._handleFileOpen);
        dispatcher.addEventListener(Event.SELECT, this._handleFileOpen);
        dispatcher.addEventListener(Event.CANCEL, this._handleFileCancel);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, this._handleFileProgress);
        dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,this._handleFileComplete);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, this._handleError);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this._handleError);
    }

where "dispatcher" is the file:

        for (var i:uint = 0; i < fileList.length; i++) {
            file = FileReference(fileList[i]);
            this._addFileListeners(file);
            this._pendingFiles.push(file);
        }

and a sample handler:

 private function _handleFileOpen(e:Event):void {
  var file:FileReference = FileReference(e.target);
  ...
 }

I'm not sure how you want to differentiate between two files with the same name. In my case, I send the files in a queue. So there's only ever 1 file being uploaded at a time. (pendingFiles).

Glenn
Thanks Glen, I do use a custom written queue, but I do need to be able to upload 2+ files at once so the problem still stands - my current implementation is pretty similar though
Darko Z
You need some method to identify them anyway, so you could even use an array. Then when you get a fileReference you can say "fileArray.indexOf(fileRef)" to find its index. That will be unique. Or if you have keys for the file, you can use an Object.
Glenn
A: 

I ended up creating my own class that manages events for each uploading file

Darko Z