admin管理员组

文章数量:1387360

I am working on an interface that allows the to access the file system on the client side. The user should be able to browse through the file system, select a directory and my system will display list of files and sub-directories of the selected directory.

I have tried using HTML5 File API, but that apparently only allows users to select files (not folders).

Any pointers/help in this direction will be appreciated.

I am working on an interface that allows the to access the file system on the client side. The user should be able to browse through the file system, select a directory and my system will display list of files and sub-directories of the selected directory.

I have tried using HTML5 File API, but that apparently only allows users to select files (not folders).

Any pointers/help in this direction will be appreciated.

Share Improve this question asked May 10, 2013 at 7:47 user1628340user1628340 9414 gold badges14 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This cannot be done in JavaScript as it would be a potential security issue. Only files selected by the user via a file dialog can be accessed using JavaScript.

Here's a pretty nice article on the File API if you haven't e across it yet.

If it's still an open issue, then let me give you a solution that might work for you.

HTML

File input for selecting a directory:

<input type="file" id="file-input" webkitdirectory="" directory=""/>

JavaScript

The following script collects all files from the given folder and from ALL sub folders. Even from sub-subfolders, etc.

$("#file-input").on("change", function(e) {
    var thefiles = e.target.files;
    $.each(thefiles, function(i, item) {
        var thefile = item;
        var reader = new FileReader();
        reader.onload = function() {
            files.push(thefile);
        };
        reader.readAsArrayBuffer(thefile);
    });

});

本文标签: HTMLJavascript Getting a List of all files and subdirectories in a given directoryStack Overflow