Because it is necessary to drag into the loading price resolution, so the first thought is the air, the code found on the internet is as follows:
Public class darg extends Sprite
{
Public Function darg ()
{
If (stage ){
Creat ();
} Else {
Addeventlistener (event. added_to_stage, creat );
}
}
Private VaR _ sp: SPRITE = new sprite ();
Private VaR _ format: array = [". jpg ",". jpg ",". JPEG ",". JPEG ",". GIF ",". GIF ",". PNG ",". SWF ",". PNG "];
Protected function creat (Event: event = NULL): void
{
_ Sp. Graphics. beginfill (0xff0000, 0.5 );
_ Sp. Graphics. drawrect (0, 0, stage. stagewidth, stage. stageheight );
_ Sp. Graphics. endfill ();
Addchild (_ Sp );
Stage. addeventlistener (nativedragevent. native_drag_enter, ondrop );
Stage. addeventlistener (nativedragevent. native_drag_drop, ondrop );
}
Private var Loader: loader = new loader;
Protected function ondrop (Event: nativedragevent): void
{
// Obtain the dragged file in array format, and specify that the dragged data is in file array format.
VaR files: array = event. clipboard. getdata (clipboardformats. file_list_format) as array;
// Obtain the first file to be dragged.
Var file: file = file (files [0]);
Switch (event. Type ){
Case nativedragevent. native_drag_enter:
If (_ format. indexof (file. Type )! =-1 ){
Nativedragmanager. acceptdragdrop (this );
}
Break;
Case nativedragevent. native_drag_drop:
If (_ format. indexof (file. Type )! =-1 ){
// Delete the uploaded Image
Loader. Unload ();
// Load the dragged Image
Loader. Load (New URLRequest (file. url ));
Removechild (_ Sp );
Addchild (loader );
}
Break;
}
}
Two issues to be aware:
1. There must be something on the stage, that is why the sprite should be added to the Code;
2. addchild is required, but not stage. addchild is added to the current scenario, rather than to the underlying stage. If you can listen to native_drag_enter on the stage, but there is no native_drag_drop event, you have not studied the specific cause. If you know, you can post it.