JavaScript:读取文件夹中的文件 [英] JavaScript: Read files in folder
问题描述
编辑:我正在尝试读取特定文件夹中的所有文件并列出其中的文件,而不是读取特定文件的内容。我只是尝试简单地创建一个FileSystemObject,它也没有做任何事情。我显示一个警告(弹出)beforfe制作FileSystemObject,然后一个警告(未显示)。所以问题在于简单地创建对象。
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.
原文:
我正在尝试读取文件夹中的所有文件使用JavaScript。
I am trying to read all the files in a folder by using JavaScript.
这是一个本地HTML文件,它不会在服务器上,所以我不能使用PHP。
It is a local HTML file, and it will not be on a server, so I can't use PHP I guess.
现在我正在尝试读取特定给定文件夹中的所有文件,但它没有做任何关于我做 FileSystemObject $ c的事情。 $ c>
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
这是我使用的代码,警报显示为2,然后停止。
Here is the code I use, The alert shows until 2, then it stops.
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.");
我这样做是否正确?
谢谢!
推荐答案
此解决方案仅适用于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" />
这篇关于JavaScript:读取文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!