Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot rely on <code>file.type</code>. A file without an extension will have a type of <code>""</code>. Save a text file with a <code>.jpg</code> extension and load it into a file control, and its type will display as <code>image/jpeg</code>. And, a folder named "someFolder.jpg" will also have its type as <code>image/jpeg</code>.</p> <p>Try to read the file with a <code>FileReader</code>. If a directory is dragged in, the <code>FileReader</code> will raise the <code>error</code> event:</p> <pre><code>var reader = new FileReader(); reader.onload = function (e) { // it's a file }; reader.onerror = function (e) { // it's a directory }; reader.readAsText(file); </code></pre> <p>Happily, in IE11, when a directory is dropped, the <code>e.dataTransfer.files</code> collection is empty.</p> <p>Chrome exposes an additional property in <code>e.dataTransfer</code> called <code>items</code> containing a collection of <code>DataTransferItem</code> objects. On each of these objects, you can call <code>item.webkitGetAsEntry()</code>, which returns an <code>Entry</code> object. The <code>Entry</code> object has properties <code>isDirectory</code> and <code>isFile</code>:</p> <pre><code>// Chrome only if (e.dataTransfer.items &amp;&amp; e.dataTransfer.items.length) { [].forEach.call(e.dataTransfer.items, function(item) { var entry = item.webkitGetAsEntry(); if (entry &amp;&amp; entry.isFile) { var file = item.getAsFile(); // same as object in e.dataTransfer.files[] // do something with the file } } } </code></pre> <p>Interestingly, in my experimentation, every folder I've looked at has had its <code>File.size % 4096</code> as zero. However, of course, some files will have that as well. <code>File.size</code> is not a reliable indicator of whether a file is actually a folder.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload