Get full file path in AIR

Normally if you want a file path in Flex to upload a file you use FileReference() with a browse(). 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 and I want the full path so save this to a SQLite database.

I searched quite some time to find the solution for this problem so I hope I save someone else his time with this explanation. In an AIR application you have an extra class called File. If you use this you get the full file path. Here is a little example code:

?View Code ACTIONSCRIPT
import mx.events.FileEvent;
private var openFile:File = new File()
 
private function openBrowseWindow():void{
	openFile.addEventListener(Event.SELECT, onOpenFileComplete);
	openFile.browse();
}
 
private function onOpenFileComplete(event:Event):void{
	trace("event: "+event.target.nativePath);
}

Author Bio

17 Comments

  1. rconceiver - April 10, 2008

    Are you using it with AIR…because with Flex it doesn’t work..any idea?

  2. Arno Manders - April 10, 2008

    Yeah this only work with AIR. In AIR the File class extends FileReference.

  3. junisulaiman - May 21, 2008

    I used your code in my Flex-Application and had this errormessage: Type (File) was not found or was not a compile-time constant..

    What should I do ?

  4. Arno Manders - May 21, 2008

    You are missing the import of the File class

    add this inside your script:

    import flash.filesystem.File;

  5. WarrenWootton - August 14, 2008

    Hi Arno,

    What does this mean:
    “I searched quite some time to find the solution for this problem so I hope I save someone else his time with this explanation. In an AIR application you have an extra class called File. If you use this you get the full file path. Here is a little example code:

    d3d5df82dc545fb4ffbe77b1913c006a000”

  6. Arno Manders - August 14, 2008

    Yeah I know that problem. On the front page of my blog you see a code example block but somehow it isn’t working in a single page view. Maybe I have to upgrade my wordpress but at this moment i have no time for that. In a few weeks I will try to fix it

  7. Madan - November 27, 2008

    I am not getting any class named File in flex3.

  8. Arno Manders - November 28, 2008

    File is a AIR only class so If you start a new project you need to start an AIR project else you don’t have the File Class

  9. Michiel - February 11, 2009

    Your using:

    trace(“event: “+event.target.nativePath);

    But you should use

    trace(“event: “+event.target.url);

    because nativePath works only on windows!

  10. Arno Manders - February 12, 2009

    What is your source Michiel because on livedocs they don’t mention this restriction

  11. Flex: FileReference / AIR: File @ If THen ELse… - November 4, 2009

    […] http://arnomanders.nl/index.php/archives/get-full-file-path-in-air/ Category: Flex You can follow any responses to this entry via RSS. Comments are currently […]

  12. Nuwan - September 4, 2010

    Of course you saved my time,
    thanks a lot!

  13. Mike - September 19, 2010

    Just what I needed. Thanks!

  14. Yenky - October 27, 2010

    really nice.. Thanks.
    event.target.nativePath works only for desktop application.

  15. Karthik - November 10, 2010

    Hey!! I wish to capture a folder name using flex. It is the only folder in its parent folder. This result i ll add to the code to access files inside this folder. Please Help!!! 🙂

Leave a reply