Search Flex Samples

Drag & Drop of a file into an AIR app (Beta 3)

one of the major changes affecting this particular scenario is the fact that flash.Desktop.DragManager has now been renamed as flash.desktop.NativeDragManager. Here is how you can achieve the drag & drop of file(s) from desktop. The app actually accepts an image file on drag & drop from desktop and displays it in an Image control.

  1. import NativeDragManager & NativeDragEvent
  2. add event listeners for NativeDragEvent.NATIVE_DRAG_ENTER and NativeDragEvent.NATIVE_DRAG_DROP
  3. In the DRAG_ENTER handler, you call NativeDragManager.acceptDragDrop(this);
  4. In the DRAG_DROP handler, you can get to the file(s) dropped using the following code…

    public function onDrop(event:NativeDragEvent):void{
    var dropfiles:Array = event.clipboard.getData( ClipboardFormats.FILE_LIST_FORMAT) as Array;

  5. Now you can loop through the files using a for… each loop as below

    for each (var file:File in dropfiles){

  6. Use the file as required in your app. Here I extract the extension of the file, check if it is an image and then load it in an Image control.

You can find the code of the application here…

Application Source

0 comments:

Related Flex Samples

Learn Flex: Flex Samples | Flex Video Tutorials Flex Examples