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:

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:

private var flr:FileReferenceList;
private function openFileReference():void{
	this.flr = new FileReferenceList();
	this.flr.addEventListener(Event.SELECT, onFrlResult);
	this.flr.browse();
} 

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

Comments

13 responses to “FileReferenceList() problem in AS3”

  1. JabbyPanda Avatar

    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 Avatar

    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 Avatar

    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 Avatar

    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:

    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. […] 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 Avatar
    ispebo

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

  7. Uday Avatar
    Uday

    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 Avatar

    You need to use file.nativePath or file.url

  9. Tamaz Avatar
    Tamaz

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

  10. Aris Avatar

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

  11. Ashok Avatar
    Ashok

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

  12. Menzoic Avatar
    Menzoic

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

  13. Uday Avatar
    Uday

    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 to Tamaz Cancel reply

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