Advertisement
Guest User

DragAndDropExampleClass

a guest
Sep 7th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. package
  2. {
  3. import flash.desktop.ClipboardFormats;
  4. import mx.managers.DragManager;
  5. import flash.events.NativeDragEvent;
  6. import flash.desktop.NativeDragManager;
  7. import flash.filesystem.File;
  8. import flash.filesystem.FileMode;
  9. import flash.filesystem.FileStream;
  10.  
  11. public class DragAndDropExampleClass
  12. {
  13.  
  14.  
  15. public function DragAndDropExampleClass()
  16. {
  17.  
  18. //called when app has initialized and is about to display
  19. private function onCreationComplete():void
  20. {
  21. //register for the drag enter event
  22. addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
  23.  
  24. //register for the drag drop event
  25. addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);
  26. }
  27.  
  28. //called when the user drags an item into the component area
  29. private function onDragIn(e:NativeDragEvent):void
  30. {
  31. //check and see if files are being drug in
  32. if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
  33. {
  34. //get the array of files
  35. var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
  36.  
  37. //make sure only one file is dragged in (i.e. this app doesn't
  38. //support dragging in multiple files)
  39. if(files.length == 1)
  40. {
  41. //accept the drag action
  42. NativeDragManager.acceptDragDrop(this);
  43. }
  44. }
  45. }
  46.  
  47. //called when the user drops an item over the component
  48. private function onDragDrop(e:NativeDragEvent):void
  49. {
  50. //get the array of files being drug into the app
  51. var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
  52.  
  53. //grab the files file
  54. var f:File = File(arr[0]);
  55.  
  56. //create a FileStream to work with the file
  57. var fs:FileStream = new FileStream();
  58.  
  59. //open the file for reading
  60. fs.open(f, FileMode.READ);
  61.  
  62. //read the file as a string
  63. var data:String = fs.readUTFBytes(fs.bytesAvailable);
  64.  
  65. //close the file
  66. fs.close();
  67.  
  68. //display the contents of the file
  69. outputField.text = data;
  70. }
  71. }
  72.  
  73.  
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement