Drag-n-Drop file into an AIR app

There has been some update with the AS3 to support AIR beta 3. I hvae recently created an AIR app using Flash that allowed drag and drop of file from the OS desktop. After some testing the following AS3 code worked:

 import flash.display.Sprite;
 import flash.text.TextField;
 import flash.filesystem.File;
 import flash.events.Event;
 import flash.events.NativeDragEvent;
 import flash.display.MovieClip;
 import flash.desktop.NativeDragManager;
 import flash.desktop.Clipboard;
 import flash.desktop.ClipboardFormats;
 public class Main extends Sprite
  private var dragTarget:Sprite = new Sprite();

  public function Main()
   addEventListener(Event.ADDED_TO_STAGE, doStage);
  private function doStage(e:Event):void
   tv_mc.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, doDragEnter);
   tv_mc.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, doDragDrop);
   tv_mc.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, doDragExit);
  private function doDragEnter(e:NativeDragEvent):void
  private function doDragDrop(e:NativeDragEvent):void
   var dropFiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
   for each (var file:File in dropFiles){
    switch (file.extension){
     case “flv” :
     path_txt.text = file.nativePath;
     case “mp4” :
     path_txt.text = file.nativePath;
     path_txt.text = “Not a recognised file format”;  
  private function doDragExit(e:NativeDragEvent):void



~ by brianchau on January 3, 2008.

12 Responses to “Drag-n-Drop file into an AIR app”

  1. Thanks!

  2. Hi Brian. I was wondering if it was possible to get the file extension from a Loader? I’m using a loader to load a file via a urlrequest but also need to access the file extension. Any ideas?

    Hope all is good.


  3. KJ, if the filename is stored as a text variable, then I would use the String class method like Split() to look for the “.” character.

  4. Thanks Brian,
    I used that method as a last resort. It just seemed a little bit untidy to me. I just assumed that LoaderInfo would contain that sort of information but it doesn’t seem to.

  5. Due to the fact that AIR’s intent is cross-OS-compatibility it is imaginable why AIR doesn’t deliver the file-ending via script-function. Under non-Windows-OS’s the file-ending is just used by some applications by parsing the file-name to quicken the check for the correct file-type. The system itself uses file-meta-descriptions or file-header-binary-information, while Windows is using the file-ending in most cases to direct the io-streaming of the files content to the right file-filter.

    Just an explanation… Thought it might be helpful to know.

    With best regards,

    Paeth.ClaudiusRaphael, A3lyphe

  6. Interesting. Thanks for that.

  7. Hi Brian,

    Firstly thank you for the insight above regarding drag and dropping in Air. Was great to see a stripped down simple code source for this to get my head around.

    Just wondering if you tell me how I can display the draged file.name property with the files extension.

    I have changed the case statement to this
    path_txt.text = file.name;

    this works however is there a way to just get the file name without the extension?


  8. Sorry Brian, I made a typo and couldnt delete the post,

    Im wanting to get the file name by iteself without the .flv extension.


    • Mitch, you can use the String class and its methods like substr() or substring() to extract the file name less the extension.

  9. […] […]

  10. thx a lot !

  11. Mil gracias parcero 🙂
    So, so thansk, after of several hour of read, try and catch… your post is the rigth answer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: