You cannot rely on file.type . A file without an extension will be of type "" . Save the text file with the extension .jpg and load it into the control file, and its type will be displayed as image/jpeg . And a folder named "someFolder.jpg" will also be of type image/jpeg .
Try to read the file using FileReader . If the directory is dragged, FileReader raises the error event:
var reader = new FileReader(); reader.onload = function (e) {
Fortunately, in IE11, when the directory is dropped, the e.dataTransfer.files collection e.dataTransfer.files empty.
Chrome provides an additional property in e.dataTransfer called items that contains a collection of DataTransferItem objects. On each of these objects, you can call item.webkitGetAsEntry() , which returns an Entry object. The Entry object has the properties isDirectory and isFile :
// Chrome only if (e.dataTransfer.items && e.dataTransfer.items.length) { [].forEach.call(e.dataTransfer.items, function(item) { var entry = item.webkitGetAsEntry(); if (entry && entry.isFile) { var file = item.getAsFile(); // same as object in e.dataTransfer.files[] // do something with the file } } }
Interestingly, in my experiments, every folder I looked at had File.size % 4096 to zero. However, of course, some files will also have this. File.size not a reliable indicator of whether a file is really a folder.
gilly3
source share