달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
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");
		}
	}
}

Posted by 료~
|