[英]HTML/Javascript: Getting a List of all files and subdirectories in a given directory
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). 我曾尝试使用HTML5文件API,但显然只允许用户选择文件(而不是文件夹)。
Any pointers/help in this direction will be appreciated. 任何指针/帮助在这方面将不胜感激。
This cannot be done in JavaScript as it would be a potential security issue. 这不能在JavaScript中完成,因为它可能是一个潜在的安全问题。 Only files selected by the user via a file dialog can be accessed using JavaScript.
只能使用JavaScript访问用户通过文件对话框选择的文件。
Here's a pretty nice article on the File API if you haven't come across it yet. 下面是在一个相当不错的文章文件API ,如果你有没有碰到过它来呢。
If it's still an open issue, then let me give you a solution that might work for you. 如果它仍然是一个悬而未决的问题,那么让我给你一个可能适合你的解决方案。
HTML HTML
File input for selecting a directory: 用于选择目录的文件输入:
<input type="file" id="file-input" webkitdirectory="" directory=""/>
JavaScript 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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.