views:

210

answers:

3

I used Flash player 10, and Flex SDK 3.4. The code as followings:

// Following comes callbacks
function imageLoadOpenCallback(evt:Event):void
{
    trace("in--open");
}

function imageLoadCompleteCallback(evt:Event):void
{
    trace("in--load");
    var fr:FileReference = evt.target as FileReference;
    trace(fr.data);
}

function imageLoadErrorCallback(evt:IOErrorEvent):void
{
    trace("in--ioerror");
}

function imageSelectCancelCallback(evt:Event):void
{
    trace("in cancel");
}

function imageSelectCallback(evt:Event):void
{
    trace("in -- select");
    for (var i:int=0; i<frl.fileList.length; i++)
    {

     frl.fileList[i].addEventListener(Event.OPEN, imageLoadOpenCallback);
     frl.fileList[i].addEventListener(Event.COMPLETE, imageLoadCompleteCallback);
     frl.fileList[i].addEventListener(IOErrorEvent.IO_ERROR, imageLoadErrorCallback);
     frl.fileList[i].load();
     trace(frl.fileList[i]);
     trace(frl.fileList[i].creationDate);
     trace(frl.fileList[i].creator);
     trace(frl.fileList[i].data);
     trace(frl.fileList[i].name);
    } 
}


// Following comes UI handlers
function onAddPictures():void
{
    var imageFilter:FileFilter = new FileFilter("Images", "*.jpg;*.png");
    frl.addEventListener(Event.SELECT, imageSelectCallback);
    frl.addEventListener(Event.CANCEL, imageSelectCancelCallback);
    frl.browse([imageFilter]);
}

Only the imageSelectCancelCallback handler get called when I select some files in the dialog. But no load/open/io_error handler get called at all. I have Google some code example, in which it used FileReference instead of FileReferenceList. I don't know the reason, could you please help me?

A: 

The main reason to use FileReferenceList instead of FileReference would be if you need to upload multiple files at once. If you only want to allow uploading one file at once, simply use FileReference.

Some clarification: imageSelectCallback(), and NOT imageSelectCancelCallback(), should get called when you select some files in the file browser AND click OK. imageSelectCancelCallback() is only called when you click Cancel.

Other than that, I never used the load() API, but I did use the upload(URLRequest) API. I am not sure what's your use case, but if you need to upload an image to a server, you should use the upload() method.

Speaking of upload events, I experienced some reliability issues when listening to Event.COMPLETE events, so I actually got better results listening to DataEvent.UPLOAD_COMPLETE_DATA.

ettore
+1  A: 

Make sure in your compiler settings for flex, that you have at least 10.0.0 for "Use a specific version".

smither
A: 

In Air the fileReference objects in fileReferenceList do not fire the complete event when doing fileList[i].load(). In a Flex project it works fine. Adobe has not responded to bug reports on this appropriately.

Greg