• Home
  • About Me
  • AIR Central
  • AS3 Libs
  • Books
  • Flex Central
  • Resources
  • The Guru's
  •  

    Simple Drag and Drop into AIR

    Drag and Drop from the desktop to an AIR application is one of the new features of AIR.

    Basically, I add the event listeners to the application to handle the drag events. Then when a file is dropped onto the application, I check the file extension and then handle it if is an image file or throw an alert if not. You can certainly update the switch statement to handle additional file extensions differently.

    Dragging In
    dd1.jpg

    After Drop
    dd2.jpg

    Here is some sample code:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
    3.   creationComplete="init()">
    4.    
    5.     <mx:Script>
    6.       <![CDATA[
    7.         import mx.controls.Alert;
    8.         import mx.controls.Image;
    9.         import flash.filesystem.File;
    10.  
    11.         private function init():void{
    12.           this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
    13.           this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
    14.         }
    15.  
    16.         private function onDragIn(event:NativeDragEvent):void{
    17.           NativeDragManager.acceptDragDrop(this);
    18.         }
    19.  
    20.         private function onDrop(event:NativeDragEvent):void{
    21.           var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
    22.           for each (var file:File in dropfiles){
    23.             switch (file.extension.toLowerCase()){   
    24.               case "png" :
    25.                 addImage(file.nativePath);
    26.                     break;
    27.               case "jpg" :
    28.                 addImage(file.nativePath);
    29.                 break;
    30.               case "jpeg" :
    31.                 addImage(file.nativePath);
    32.                 break;
    33.               case "gif" :
    34.                 addImage(file.nativePath);
    35.                 break;
    36.               default:
    37.                 Alert.show("Unmapped Extension");
    38.               }
    39.             }
    40.           }
    41.  
    42.           private function addImage(nativePath:String):void{
    43.             var i:Image = new Image();
    44.             if(Capabilities.os.search("Mac") >= 0){
    45.               i.source = "file://" + nativePath;
    46.             } else {
    47.               i.source = nativePath;
    48.             }
    49.             this.addChild(i);
    50.           }
    51.  
    52.         ]]>
    53.     </mx:Script>
    54.    
    55. </mx:WindowedApplication>

    2 Responses to “Simple Drag and Drop into AIR”

    1. mark johnson Says:

      using the following to test the file extension would shorten your code a bit:

      if ( “”.indexOf( “”) > -1) { addImage(file.nativePath);
      }

    2. mark johnson Says:

      hmm… your comment system removed the good stuff…

    Leave a Reply