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!

Be Sociable, Share!
Tagged with: , ,
Posted in Actionscript, Flash

4 comments on “FileReference.load() – keep it in scope!
  1. Ian Ford says:

    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.

  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.

  3. Greg says:

    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.

  4. Kasper says:

    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

1 Pings/Trackbacks for "FileReference.load() – keep it in scope!"
  1. [...] FileReference.load() – keep it in scope! at calm in the chaos blog.wrench.com.au/2010/06/16/filereferenceload-keep-it-in-scope/ – view page – cached Flash, Flex and Technology Tweets about this link Topsy.Data.Twitter.User['mojogeeks'] = {“photo”:”http://a3.twimg.com/profile_images/476530319/twitterProfilePhoto_normal.jpg”,”url”:”http://twitter.com/mojogeeks”,”nick”:”mojogeeks”}; mojogeeks: “FileReference.load() – keep it in scope!: Just a quick note (possibly to self) that when dealing with FileReferenc… http://bit.ly/cVQ6XA ” 3 hours ago retweet Topsy.Data.Twitter.User['fmc_washington'] = {“photo”:”http://a3.twimg.com/profile_images/918759969/GSA_MASC_normal.jpg”,”url”:”http://twitter.com/fmc_washington”,”nick”:”fmc_washington”}; fmc_washington: “FileReference.load() – keep it in scope!: Just a quick note (possibly to self) that when dealing with FileReferenc… http://bit.ly/brxTVU ” 3 hours ago retweet Topsy.Data.Twitter.User['fmc_chicago'] = {“photo”:”http://a1.twimg.com/profile_images/844354384/23425_109532382416879_100000803546890_75533_7952061_n_normal.jpg”,”url”:”http://twitter.com/fmc_chicago”,”nick”:”fmc_chicago”}; fmc_chicago: “FileReference.load() – keep it in scope!: Just a quick note (possibly to self) that when dealing with FileReferenc… http://bit.ly/brxTVU ” 3 hours ago retweet Topsy.Data.Twitter.User['geekguypc'] = {“photo”:”http://a1.twimg.com/profile_images/960752858/Sin_t_tulo-1_normal.png”,”url”:”http://twitter.com/geekguypc”,”nick”:”geekguypc”}; geekguypc: “FileReference.load() – keep it in scope! http://dlvr.it/1lXys ” 3 hours ago retweet Filter tweets [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posts this month
June 2010
M T W T F S S
« May   Aug »
 123456
78910111213
14151617181920
21222324252627
282930  
Archives