I'm searching for a cross browser solution for this. In advance i tell you that i don't want to know if is a file only, because i found solutions for that. I want to know if the file is an audio, image or video basically. In Chrome when you fire the dragenter event you can get that data from here:
ev.originalEvent.dataTransfer.items[0].type;
But in Firefox, Safari and IE the "items" spec hasn't been applyed yet: https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items
In those browsers you can only see the "files" attribute:
ev.originalEvent.dataTransfer.files[0];
But on dragenter files[0]
is empty. How can i workaround this to know the file type in these other browsers?
Example (Only works on Chrome):
$(document).on('dragenter', '.drop-zone', function(ev) {
var e = ev.originalEvent;
e.dataTransfer.dropEffect = 'copy';
var file = e.dataTransfer.items[0];
var type = file.type.slice(0, file.type.indexOf('/'));
$(ev.target).html(type);
});
$(document).on('dragleave', '.drop-zone', function(ev) {
$(ev.target).html('Drag your file over here to know the type, no need to drop it');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="drop-zone">Drag your file over here to know the type, no need to drop it</div>