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:

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);
}

Comments

17 responses to “Get full file path in AIR”

  1. rconceiver Avatar
    rconceiver

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

  2. Arno Manders Avatar

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

  3. junisulaiman Avatar
    junisulaiman

    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 Avatar

    You are missing the import of the File class

    add this inside your script:

    import flash.filesystem.File;

  5. WarrenWootton Avatar
    WarrenWootton

    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 Avatar

    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 Avatar
    Madan

    I am not getting any class named File in flex3.

  8. Arno Manders Avatar

    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 Avatar
    Michiel

    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 Avatar

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

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

    Of course you saved my time,
    thanks a lot!

  13. Mike Avatar
    Mike

    Just what I needed. Thanks!

  14. Yenky Avatar

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

  15. Karthik Avatar
    Karthik

    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!!! 🙂

  16. Livin Avatar
    Livin

    Thanks A Lot!

Leave a Reply

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