Javascript 如何在javascript中从本地PC获取目录的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13624450/
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
How to get the contents of the directory from local PC in javascript
提问by Marcin Kostrzewa
How to get the contents of the directory from local PC in javascript/jQuery?
For example from C:\Images
如何在 javascript/jQuery 中从本地 PC 获取目录的内容?例如从C:\Images
回答by Esailija
This only works in google chrome:
这仅适用于谷歌浏览器:
<input type="file" webkitdirectory>
This will prompt the user to select a directory and you can then access the filesproperty of the input to see the contained files.
这将提示用户选择一个目录,然后您可以访问files输入的属性以查看包含的文件。
It is then possible to use the File System APIto construct a virtual, sandboxed file system of the user selected files and have programmatic access to this virtual filesystem as if it was a real filesystem accessed by desktop app.
然后可以使用文件系统 API为用户选择的文件构建一个虚拟的沙盒文件系统,并对该虚拟文件系统进行编程访问,就好像它是桌面应用程序访问的真实文件系统一样。
There is no way otherwise because that would be a big security issue
没有别的办法,因为那将是一个很大的安全问题
Working demo in google chrome: http://jsfiddle.net/JwgqC/
谷歌浏览器中的工作演示:http: //jsfiddle.net/JwgqC/
回答by echo_Me
here you can read local files by html5 and javascript using the file APIS
在这里,您可以使用文件 APIS 通过 html5 和 javascript 读取本地文件
回答by Shree
Javascript/Jquery does not have access to the local file system for security reasons. This is not possible.So try some server side base code.
出于安全原因,Javascript/Jquery 无权访问本地文件系统。这是不可能的。所以尝试一些服务器端基本代码。
回答by Plaute
Now, some years later, accessing to local filesworks fine in Chrome ANDFirefox (52.8, but update Firefox is easy). It works also with IE 11.
现在,几年后,在 Chrome和Firefox 中访问本地文件可以正常工作(52.8,但更新 Firefox 很容易)。它也适用于 IE 11。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>files</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<script>
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
</script>
</body>
</html>
回答by czerasz
You could write a script using a technology like php, ruby, java which will list all files and then send this information via ajax to Your browser.
您可以使用 php、ruby、java 等技术编写脚本,该脚本将列出所有文件,然后通过 ajax 将此信息发送到您的浏览器。
But You can't do this only via javascript because of security restrictions.
但是由于安全限制,您不能仅通过 javascript 执行此操作。
回答by slowe
I don't think this is possible because accessing the local file system is a terrible security risk.
我认为这是不可能的,因为访问本地文件系统是一个可怕的安全风险。
回答by looper
Javascript can't access the users drive. That would be a big security issue, wouldn't it? :D
Javascript 无法访问用户驱动器。那将是一个很大的安全问题,不是吗?:D
Instead, you need to use an other technology like flash or java-applets.
相反,您需要使用其他技术,如 flash 或 java-applet。
回答by hereandnow78
it is not possible to have javascript-access to the local file-system from the browser, and this is good so!
不可能从浏览器对本地文件系统进行 javascript 访问,这很好!

