FileReferenceList() problem in AS3

Today I noticed a strange thing in Flex. Let me explain the problem.

I created FileReferenceList. I added a eventListener to that FileReferenceList that returns me the list of files that the user selects. This is the code:

?View Code ACTIONSCRIPT
private function openFileReference():void{
	var flr:FileReferenceList = new FileReferenceList();
	flr.addEventListener(Event.SELECT, onFrlResult);
	flr.browse();} 
 
private function onFrlResult(event:Event):void{
	trace("Select event: "+event.target);
}

When I tested the code I noticed that the function wasn’t called.

After that I tried to change the “FileReferenceList” to a “FileReference” and for the rest I kept the function the same. In this case my function “onFrlResult” was called. After some try and error I found the solution.

You can create a FileReference in a function but this is different with a FileReferenceList. You have to declare a variable with the type FileReferenceList outside the function. The final code will look like this:

?View Code ACTIONSCRIPT
private var flr:FileReferenceList;
?View Code ACTIONSCRIPT
private function openFileReference():void{
	this.flr = new FileReferenceList();
	this.flr.addEventListener(Event.SELECT, onFrlResult);
	this.flr.browse();
?View Code ACTIONSCRIPT
} 
 
private function onFrlResult(event:Event):void{
	trace("Select event: "+event.target);
}

I’m not sure if it is a problem/bug. Maybe there is a reason that you can’t create a variable with the type FileReferenceList inside a function. If someone knows why than please share it with me and leave a comment

Author Bio

13 Comments

  1. JabbyPanda - March 21, 2008

    You are all right, you have to declare the variable of FileReferenceList in the class outside of the function in order to receive the Event.SELECT at your event handler.

    That’s how it works….

    See this blog entry with the reference to livedocs documentation:
    http://my.opera.com/darylducharme/blog/2007/05/17/losing-filereference-scope

  2. Arno Manders - March 21, 2008

    Thanx for the link. Only difference that he has the problem with FileReference and I have the problem with FileReferenceList. I also tested it with FileReference and that was working fine for me

  3. Arul Prasad - March 25, 2008

    This happens because the variable inside the function loses scope, by the time the event is triggered. If your application had entered some other code block by the time the select event is triggered, the local variable flr becomes an unknown variable, and hence a listener added to that method is lost as well.

  4. Arno Manders - March 25, 2008

    you’re right.

    Before I wrote this article I tested it but I did something wrong I guess. When I tested it the event got triggered with FileReference but not with FileReferenceList. Now I test it again both didn’t get triggered.

    So in both cases you should declare the FileReference or FileReferenceList outside a function

    or you could do it like this:

    ?View Code ACTIONSCRIPT
    private function FileReferenceCall():void{
    	var frl:FileReferenceList = new FileReferenceList();
    	frl.addEventListener(Event.SELECT, onSelectFrl);
    	frl.browse();
    	function onSelectFrl(event:Event):void{
    		trace("Event: "+event.currentTarget);
    	}
    }
  5. Arno Manders » Blog Archive » Get full file path in AIR - April 1, 2008

    […] Last week I tried the same technique to get a file dragged from the desktop to my AIR application. After fixing a strange problem the code worked but the next problem appeared to me. With FileReference I only get the file name […]

  6. ispebo - May 20, 2008

    :FileReferenceList have to be GLOBAL and not a LOCAL VARIABLE 🙂

  7. Uday - June 21, 2008

    I am not able to trace the selected file’s location.
    we are using trace(file.name);
    it traces: abc.jpt.
    While we need the location of this file:
    like: C://images/abc.jpg

    Can you help me plz.

    Thanks,
    Uday

  8. Arno Manders - June 22, 2008

    You need to use file.nativePath or file.url

  9. Tamaz - March 12, 2009

    Great post, thanks, helped me out! I’ve been thinking for a while, why it didnt work=)

  10. Aris - December 30, 2009

    Thanks buddy. This helped me out. Before that I was staring at the code and it looked fine….

  11. Ashok - May 23, 2010

    Thanks! I was struggling with the same problem until I saw your post.

  12. Menzoic - July 6, 2010

    You can just declare an array as a member variable, and then push a new FileReferenceList to that array within any function.

  13. Uday - January 26, 2012

    Any one can help me my old post:
    I am not able to trace the selected file’s location.
    I am using trace(file.name);
    it traces: abc.jpt.
    While we need the exact location of this file:
    like: C://images/abc.jpg

    Can any one help for the same.

    As some one suggested to use:
    file.nativePath or file.url

    It’s only for AIR but I need it for online.

    Thanks,
    Uday

Leave a reply