Get full file path in AIR

1 Apr

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

Related posts:

11 Responses to “Get full file path in AIR”

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

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

  2. Arno Manders 10. Apr, 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. Aug, 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. Aug, 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. Nov, 2008 at 12:27 #

    I am not getting any class named File in flex3.

  8. Arno Manders 28. Nov, 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. Feb, 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. Feb, 2009 at 20:47 #

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

Trackbacks and Pingbacks

  1. Flex: FileReference / AIR: File @ If THen ELse… - 04. Nov, 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