Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect directory select capability in browsers?

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?

like image 722
mvbl fst Avatar asked Aug 29 '12 01:08

mvbl fst


People also ask

Can browser Access file System?

Web browsers (and JavaScript) can only access local files with user permission. To standardize the file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.

What is the proper way to conduct feature detection?

Another good approach is to encapsulate feature detection into a set of functions that can then be used throughout the code. Here's a best practice for detecting whether the browser supports the HTML5 <canvas> element and if so, makes sure that the canvas. getContext('2d') method is working as well.

How do I access files from my browser?

Using Google Chrome to access local files is as easy as pressing Ctrl + O at the same time. This interface will open, allowing you to navigate to whichever file or folder is needed. There are several types of files which can be opened using Chrome. These include pdf, mp3 files, some video files and most document files.


1 Answers

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;
}
like image 124
twalthr Avatar answered Sep 25 '22 05:09

twalthr