Javascript 是否可以使用javascript从目录中读取文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11332042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to read files from a directory using javascript?
提问by aF.
I want to read a directory and fill a list with the name of those files.
我想读取一个目录并用这些文件的名称填充一个列表。
Is it possible to do this tasks using javascript?
是否可以使用 javascript 完成此任务?
回答by Widor
No, for security reasons.
不,出于安全原因。
You might be able to do it by invoking ActiveX or Flash and having the user agree to permit file system access from these plugins, but - please don't.
您可以通过调用 ActiveX 或 Flash 并让用户同意允许从这些插件访问文件系统来实现,但是 - 请不要这样做。
回答by antyrat
You can try to use FileReader
object, but it poorly supported by browsers.
您可以尝试使用FileReader
对象,但浏览器对它的支持很差。
回答by raina77ow
No, it's not possible. Would you like someone to read the contents of yourdirectories?
不,这不可能。您希望有人阅读您目录的内容吗?
UPDATE: I suppose the closest you might get by using webkitdirectory
attribute:
更新:我想您可能会通过使用webkitdirectory
属性获得最接近的结果:
HTML
HTML
<input type="file" id="file_input" webkitdirectory="" directory="" />
<div id="list_of_files"></div>
...
JS
JS
var $list = $('#list_of_files');
$('#file_input').change(function(event) {
var listOfFiles = event.target.files;
for (var i = 0, l = listOfFiles.length; i < l; ++i) {
$list.append($('<p>'+ listOfFiles[i].name +'</p>'));
}
});
... as shown here. But it works in Chrome only (and suggested usage of mozdirectory
attribute didn't help).
...如图所示这里。但它仅适用于 Chrome(并且建议使用mozdirectory
属性没有帮助)。
回答by Esailija
In google chrome you may prompt the client to select a directory and then use this to list the files contained in the directory and its subdirectories:
在谷歌浏览器中,您可能会提示客户端选择一个目录,然后使用它来列出该目录及其子目录中包含的文件:
<input type="file" webkitdirectory onchange="listContents(this.files)">
listContents
would be your implementation.
listContents
将是你的实现。
回答by George Katsanos
I don't know if you're doing security research etc etc.. So besides from saying "you shouldn't do it", the actual answer to the question is, you can actually READ files by taking advantage poorly-written JS code, that's why you should code.. defensively.
我不知道你是否在做安全研究等等。所以除了说“你不应该这样做”之外,这个问题的实际答案是,你实际上可以通过利用写得不好的 JS 代码来读取文件,这就是为什么你应该编码......防御性。
Then there's this: http://www.html5rocks.com/en/tutorials/file/dndfiles/
然后是这个:http: //www.html5rocks.com/en/tutorials/file/dndfiles/
回答by Future2020
Yes depending on the browser you have.
是的,取决于您拥有的浏览器。
Even though it is not a common practice but you can using certain browsers such as Chrome (using the requestFileSystem supported by webkitRequestFileSystem) or in Internet Explorer using File System Object.
尽管这不是一种常见的做法,但您可以使用某些浏览器,例如 Chrome(使用 webkitRequestFileSystem 支持的 requestFileSystem)或在 Internet Explorer 中使用文件系统对象。