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

no related content

Tags: , , , , , , , , , ,

13 Responses to “FileReferenceList() problem in AS3”

  1. JabbyPanda 21 March, 2008 at 20:55 #

    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 21 March, 2008 at 21:02 #

    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 25 March, 2008 at 04:49 #

    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 25 March, 2008 at 20:11 #

    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. ispebo 20 May, 2008 at 13:58 #

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

  6. Uday 21 June, 2008 at 19:50 #

    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

  7. Arno Manders 22 June, 2008 at 11:51 #

    You need to use file.nativePath or file.url

  8. Tamaz 12 March, 2009 at 14:51 #

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

  9. Aris 30 December, 2009 at 00:02 #

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

  10. Ashok 23 May, 2010 at 00:59 #

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

  11. Menzoic 6 July, 2010 at 23:23 #

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

  12. Uday 26 January, 2012 at 21:11 #

    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

Trackbacks/Pingbacks

  1. Arno Manders » Blog Archive » Get full file path in AIR - 1 April, 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 [...]

Leave a Reply