JavaScript:读取文件夹中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7022058/
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
JavaScript: Read files in folder
提问by Killerwes
EDIT:I'm trying to read all the files in a specific folder and list the files in there, not read the content of a specific file. I just tried to simply create an FileSystemObject and it doesn't do anything either. I show an alert (which pops up) beforfe making the FileSystemObject, and one after it (which isn't shown). So the problem is in simply creating the object.
编辑:我试图读取特定文件夹中的所有文件并列出其中的文件,而不是读取特定文件的内容。我只是试图简单地创建一个 FileSystemObject 并且它也没有做任何事情。我在创建 FileSystemObject 之前显示一个警报(弹出),然后显示一个(未显示)。所以问题在于简单地创建对象。
Original:
原来的:
I am trying to read all the files in a folder by using JavaScript.
我正在尝试使用 JavaScript 读取文件夹中的所有文件。
It is a local HTML file, and it will not be on a server, so I can't use PHP I guess.
它是一个本地 HTML 文件,它不会在服务器上,所以我猜我不能使用 PHP。
Now I'm trying to read all the files in a specific given folder, but it doesn't do anything on the point I make a FileSystemObject
现在我正在尝试读取特定给定文件夹中的所有文件,但它在我做的点上没有做任何事情 FileSystemObject
Here is the code I use, The alert shows until 2, then it stops.
这是我使用的代码,警报显示到 2,然后停止。
alert('1');
var myObject, afolder, date;
alert('2');
myObject = new ActiveXObject("Scripting.FileSystemObject");
alert('3');
afolder = myObject.GetFolder("c:\tmp");
alert('4');
date = afolder.DateLastAccessed;
alert("The folder"+name+" is a temporary folder.");
Am I doing this the right way?
我这样做是否正确?
Thanks!
谢谢!
回答by Corey Alexander
The method I found with a Google search uses HTML5 so if you are using a modern browser you should be good. Also the tutorial page seems to check if the browser you are using supports the features. If so you should be good to follow the tutorial which seems pretty thorough.
我在 Google 搜索中找到的方法使用 HTML5,因此如果您使用的是现代浏览器,那应该很好。此外,教程页面似乎会检查您使用的浏览器是否支持这些功能。如果是这样,您应该很好地遵循看起来非常彻底的教程。
回答by BelgoCanadian
This solution only works on IE11 or older since it is MS based
此解决方案仅适用于 IE11 或更早版本,因为它基于 MS
<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
function showFolderFileList(folderspec) {
var s = "";
var f = fso.GetFolder(folderspec);
// recurse subfolders
var subfolders = new Enumerator(f.SubFolders);
for(; !subfolders.atEnd(); subfolders.moveNext()) {
s += ShowFolderFileList((subfolders.item()).path);
}
// display all file path names.
var fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext()) {
s += fc.item() + "<br>";
}
return s;
}
function listFiles() {
document.getElementById('files').innerHTML = showFolderFileList('C:');
}
</script>
<input type='button' onclick='listFiles()' value='List Files' />
<div id="files" />