header image
 

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:

package
{
 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
  {
   NativeDragManager.acceptDragDrop(tv_mc);
  }
  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;
     break;
     case “mp4″ :
     path_txt.text = file.nativePath;
     break;
     default:
     path_txt.text = “Not a recognised file format”;  
    }
   }
  }
  private function doDragExit(e:NativeDragEvent):void
  {
   
  }
 }
}

Enjoy!

~ by brianchau on January 3, 2008.

4 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.

    KJ

  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.

Leave a Reply