如何在javascript/jQuery中获取本地PC目录的内容?例如从
C:\Images
。C:\Images
。这仅适用于Google Chrome浏览器:
<input type="file" webkitdirectory>
这将提示用户选择目录,然后您可以访问输入的files
属性以查看其中包含的文件。在这里,您可以使用文件API和HTML5以及JavaScript读取本地文件。
现在,几年过去了,在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>
您可以使用像PHP、Ruby、Java这样的技术编写脚本,列出所有文件,然后通过Ajax将此信息发送到您的浏览器。
但是,由于安全限制,您无法仅使用JavaScript完成此操作。
我认为这是不可能的,因为访问本地文件系统会带来严重的安全风险。
Javascript无法访问用户的驱动器。那将是一个很大的安全问题,不是吗?:D
相反,您需要使用其他技术,如Flash或Java小程序。
从浏览器访问本地文件系统是不可能的,这也是好事!