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);
}
no related content

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

17 Responses to “Get full file path in AIR”

  1. rconceiver 10 April, 2008 at 10:36 #

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

  2. Arno Manders 10 April, 2008 at 10:38 #

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

  3. junisulaiman 21 May, 2008 at 17:06 #

    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 21 May, 2008 at 17:47 #

    You are missing the import of the File class

    add this inside your script:

    import flash.filesystem.File;

  5. WarrenWootton 14 August, 2008 at 17:44 #

    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 14 August, 2008 at 22:07 #

    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 27 November, 2008 at 12:27 #

    I am not getting any class named File in flex3.

  8. Arno Manders 28 November, 2008 at 20:43 #

    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 11 February, 2009 at 02:03 #

    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 12 February, 2009 at 20:47 #

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

  11. Nuwan 4 September, 2010 at 17:28 #

    Of course you saved my time,
    thanks a lot!

  12. Mike 19 September, 2010 at 18:27 #

    Just what I needed. Thanks!

  13. Yenky 27 October, 2010 at 09:07 #

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

  14. Karthik 10 November, 2010 at 07:42 #

    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!!! :)

  15. Arno Manders 10 November, 2010 at 09:02 #

    Hey Karthik. I think you mean something like this: http://cookbooks.adobe.com/post_Recursive_folder_listings_and_contents_processing-9410.html

    good luck with it

  16. Livin 22 September, 2011 at 08:26 #

    Thanks A Lot!

Trackbacks/Pingbacks

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

    [...] http://blog.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 [...]

Leave a Reply