How to detect directory select capability in browsers?
Asked Answered
D

2

7

I am trying to find out if browser has ability to select folders, not just multiple files. Current Chrome supports this (example: http://html5-demos.appspot.com/static/html5storage/demos/upload_directory/index.html).

Apparently, it works in Chrome when <input type="file" /> has webkitdirectory attribute. But how can I test if browser is actually capable of selecting folders and iterating through files?

Debug answered 29/8, 2012 at 1:3 Comment(3)
possible duplicate of How can I check if the browser support HTML5 file upload (FormData object)?Itching
It's not a duplicate. As far as I know, only Chrome supports webkitdirectory (or the future directory attribute) while other browsers currently support HTML5 File API. There is a need for such a test, as the Chrome <input type="file" webkitdirectory /> allows only folders to be selected, not files or folders.Serosa
Hey @GeoffreyBooth -- I already wrote Modernizr plugin for this.Debug
M
13

Maybe this is a solution for your problem:

function isInputDirSupported() {
    var tmpInput = document.createElement('input');
    if ('webkitdirectory' in tmpInput 
        || 'mozdirectory' in tmpInput 
        || 'odirectory' in tmpInput 
        || 'msdirectory' in tmpInput 
        || 'directory' in tmpInput) return true;

    return false;
}
Munch answered 19/2, 2013 at 22:36 Comment(2)
Thanks for answer. This is pretty much what we did in a custom Modernizr test.Debug
This return true in Android Chrome, but its input length will not change after selected this file.Highclass
B
0
'webkitdirectory' in HTMLInputElement.prototype

I think this is sufficient in modern browsers.

Belly answered 18/7 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.