FileReference.load() – keep it in scope!

Just a quick note (possibly to self) that when dealing with FileReference you need to ensure that the scope of the FileReference is maintained. This was mentioned in the Actionscript documentation, but it took me a while to figure out that that could be the problem for my code failing silently. Originally I thought it was because I wasn’t using the right playerglobal.swc from within Flex, or that I couldn’t export using Flex SDK 3.2 (as this method is a Flash Player 10 one). It took me a while to reread the documentation for the load method and comprehend that perhaps I needed to declare some instance variables instead of local ones. Some code will hopefully illustrate what I mean:

public function uploadImage():void
{
	var __imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
	var __fileRef:FileReference = new FileReference();
	try	{
		__fileRef.addEventListener(Event.SELECT, onFileSelect);
		__fileRef.addEventListener(Event.COMPLETE, onFileOpen);
		__fileRef.browse([__imageTypes]);
	} catch (error:Error) {
		trace("Unable to browse for files.");
	}
}

private function onFileSelect(event:Event):void
{
	trace("onFileSelect called");
	var fileRef:FileReference = event.target as FileReference;
	fileRef.load();
}

private function onFileOpen(event:Event):void
{
	trace("onFileOpen called");
	var fileRef:FileReference = event.target as FileReference;
	var data:ByteArray = fileRef.data as ByteArray;
	var encoder:Base64Encoder = new Base64Encoder();
	encoder.encodeBytes(data);
	trace(encoder.toString())
}

The problem with that code is that the original __fileRef variable is only available within the uploadImage function. As soon as onFileSelect is triggered (which works fine), fileRef (a new local variable within this function) trys to call load(). This was failing silently for me.

So instead of declaring the original __fileRef variable as a local one, I’ve declared it as a private variable in the class. That way it’s always in scope!

5 thoughts on “FileReference.load() – keep it in scope!”

  1. But that’s quite odd…Why should scope have anything to do with the issue? In the case of your “onFileSelect” function, you’re calling your load() method on the very same FileReference as the one you previously added your Event.COMPLETE handler to.

    In fact, I just tested your code in FlashCS4 and had no trouble getting both the onFileSelect() and onFileOpen() functions to be called.

    Reply
  2. @Ian – I did have some success when I debugged in Flex, so thought it may have been a timing issue with the loading… and you’re right, I was pretty sure I was passing the reference to the original variable (keeping it in scope) by using event.target The only difference between my and your setup is that I was using CS3 to compile.

    Reply
  3. Works fine unless your doing an Air project. The fileList objects in FileReferenceList don’t fire the complete event on fileList[i].load(). Adobe has not taken bug reports on this appropriately.

    Reply
  4. Some more info on this bug the google’s out there, just finished spending a like 6 hours on this bug when it suddenly appeared out of no where.

    Make sure the parent class to you file-reference exists and is not garbage disposed to early by setting having a reference to the parent class somewhere.

    I am pretty sure this is because in my case the parent class had no reference from the displaylist or main class, and then got collected because flash did not think it was being used.

    //Sebb

    Reply

Leave a comment