Darko
Darko

Reputation: 38860

Flex 3 multiple upload progress monitoring

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.

Upvotes: 2

Views: 2125

Answers (3)

Darko
Darko

Reputation: 38860

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

Upvotes: 0

Cameron
Cameron

Reputation: 98756

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');

Upvotes: 1

Glenn
Glenn

Reputation: 5342

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).

Upvotes: 1

Related Questions