After you create a custom drop target on your desktop, you can drag and drop the file from the desktop onto that target. Similar to dragging and dropping a picture or link, dropping the file from the desktop onto the browser triggers the drop event. It is also possible to read the placed file in Event.dataTransfer.files, which is a file object, just like the file object obtained through the input field.
Example:
<! DOCTYPE html>
<style type= "Text/css" >
#dropTarget {width:200px height:200px; background: #ccc; border:1px solid #999;}
</style>
<body>
<p> Please drag the file into the box below:</p>
<div id= "DropTarget" ></div>
<div id= "Output" ></div>
<script type= "Text/javascript" >
function Draghandle (event) {
var info = "",
Outputele = document.getElementById ("Output"),
Files, I, Len;
Event.preventdefault ();
if (Event.type = "Drop") {
Files = event.dataTransfer.files;
i = 0;
len = files.length;
while (I < Len) {
Info + + Files[i].name + "(" + Files[i].type + "," + files[i].size + "bytes) <br>";
i++;
}
outputele.innerhtml = info;
}
}
var Droptargetele = document.getElementById ("DropTarget");
Droptargetele.addeventlistener ("DragEnter", Draghandle, false);
Droptargetele.addeventlistener ("DragOver", Draghandle, false);
Droptargetele.addeventlistener ("Drop", Draghandle, false);
</script>
</body>
As with other drag-and-drop examples, the default behavior of DragEnter, DragOver, and drop must also be canceled here.
In the drop event, file information can be read by Event.dataTransfer.files.
The ability to read drag-and-drop files commonly used scenarios are:
Combine XMLHttpRequest and drag-and-drop files to upload. Up
View and manage local pictures.
View and manage local music.