![](/img/trans.png)
[英]Using browser javascript, how to get the contents of the files and subfolders of the user-specified local directory
[英]How to get the contents of the directory from local PC in javascript
如何在 javascript/jQuery 中從本地 PC 獲取目錄的內容? 例如從C:\Images
這只適用於谷歌瀏覽器:
<input type="file" webkitdirectory>
這將提示用戶選擇一個目錄,然后您可以訪問輸入的files
屬性以查看包含的文件。
然后可以使用文件系統 API為用戶選擇的文件構建一個虛擬的沙盒文件系統,並以編程方式訪問該虛擬文件系統,就好像它是桌面應用程序訪問的真實文件系統一樣。
沒有別的辦法,因為那將是一個很大的安全問題
谷歌瀏覽器中的工作演示:http: //jsfiddle.net/JwgqC/
在這里你可以使用文件 APIS 通過 html5 和 javascript 讀取本地文件
出於安全原因,Javascript/Jquery 無法訪問本地文件系統。 這是不可能的。所以嘗試一些服務器端基本代碼。
現在,幾年后,訪問本地文件在 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-applets。
不可能從瀏覽器使用 javascript 訪問本地文件系統,這很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.