Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.desktop.ClipboardFormats;
- import mx.managers.DragManager;
- import flash.events.NativeDragEvent;
- import flash.desktop.NativeDragManager;
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- public class DragAndDropExampleClass
- {
- public function DragAndDropExampleClass()
- {
- //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
- NativeDragManager.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;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement