FileReferenceList() problem in AS3
21 Mar
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
Related posts:

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
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
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.
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:
:FileReferenceList have to be GLOBAL and not a LOCAL VARIABLE :)
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
You need to use file.nativePath or file.url
Great post, thanks, helped me out! I’ve been thinking for a while, why it didnt work=)
Thanks buddy. This helped me out. Before that I was staring at the code and it looked fine….
Thanks! I was struggling with the same problem until I saw your post.
You can just declare an array as a member variable, and then push a new FileReferenceList to that array within any function.