DragAndDropExampleClass.as
import flash.desktop.ClipboardFormats; import flash.desktop.DragManager; import flash.events.NativeDragEvent; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; //called when app has initialized and is about to display private function onCreationComplete():void { //register for the drag enter event addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn); //register for the drag drop event addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop); } //called when the user drags an item into the component area private function onDragIn(e:NativeDragEvent):void { //check and see if files are being drug in if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) { //get the array of files var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; //make sure only one file is dragged in (i.e. this app doesn't //support dragging in multiple files) if(files.length == 1) { //accept the drag action DragManager.acceptDragDrop(this); } } } //called when the user drops an item over the component private function onDragDrop(e:NativeDragEvent):void { //get the array of files being drug into the app var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; //grab the files file var f:File = File(arr[0]); //create a FileStream to work with the file var fs:FileStream = new FileStream(); //open the file for reading fs.open(f, FileMode.READ); //read the file as a string var data:String = fs.readUTFBytes(fs.bytesAvailable); //close the file fs.close(); //display the contents of the file outputField.text = data; }
One of the cool things about the API, is that you can have specific components within your application accept native drag and drop operations (and not just your entire app).
Note, right now, the app is not doing any checking to confirm that the file is a text file. If you try and drag a binary file into the app you will get weird results. (Im looking into how to check that the file is text based).
출처 : http://www.mikechambers.com/blog/2007/11/07/air-example-native-drag-and-drop/
드롭된 이미지 파일의 확장자별로 분류
public function onDrop(event:NativeDragEvent):void { DragManager.dropAction = DragActions.COPY; var dropfiles:Array = event.transferable.dataForFormat(TransferableFormats.FILE_LIST_FORMAT) as Array; for each (var file:File in dropfiles) { switch (file.extension) { case "png" : addImage(file.nativePath); break; case "jpg" : addImage(file.nativePath); break; case "gif" : addImage(file.nativePath); break; default: Alert.show("Unmapped Extension"); } } }
'Action Script 3.0 > Codes / Tips' 카테고리의 다른 글
Away3D 4.0 alpha Cube.tile6 버그 수정 (0) | 2011.08.27 |
---|---|
모듈러 연산자(%) 속도 개선 (0) | 2011.08.05 |
Abstract Class (추상클래스) 사용하기 (0) | 2011.06.04 |
숫자를 세자리 콤마(,)로 (0) | 2011.06.01 |
Air에서 외부 exe 실행 (0) | 2010.05.21 |