I\'m using the html5 events to enable both file and element drag-and-drop. I\'ve attached the dragover event to the body and am using event delegations to show where a dragg
Use the function below to check if the drag source is an external file.
Tested on Windows 7 with:
- Firefox version 39
- Chrome version 44
- Safari version 5.1.7
function isDragSourceExternalFile(dataTransfer){
// Source detection for Safari v5.1.7 on Windows.
if (typeof Clipboard != 'undefined') {
if (dataTransfer.constructor == Clipboard) {
if (dataTransfer.files.length > 0)
return true;
else
return false;
}
}
// Source detection for Firefox on Windows.
if (typeof DOMStringList != 'undefined'){
var DragDataType = dataTransfer.types;
if (DragDataType.constructor == DOMStringList){
if (DragDataType.contains('Files'))
return true;
else
return false;
}
}
// Source detection for Chrome on Windows.
if (typeof Array != 'undefined'){
var DragDataType = dataTransfer.types;
if (DragDataType.constructor == Array){
if (DragDataType.indexOf('Files') != -1)
return true;
else
return false;
}
}
}
Example Usage with JQuery
$(document).on('dragover', function(e){
var IsFile = isDragSourceExternalFile(e.originalEvent.dataTransfer);
console.log(IsFile);
});