如何列出文件夹中的文件
How to list files in folder
如何使用 Meteor
列出文件夹中的所有文件。我的应用程序上安装了 FS
collection 和 cfs:filesystem
。我没有在文档中找到它。
简短的回答是 FS.Collection 创建了一个 Mongo collection,您可以像对待其他任何东西一样对待它,即,您可以使用 find()
列出条目。
长答案...
使用 cfs:filesystem,您可以创建一个 mongo 数据库来镜像服务器上的给定文件夹,如下所示:
// in lib/files.js
files = new FS.Collection("my_files", {
stores: [new FS.Store.FileSystem("my_files", {"~/test"})] // creates a ~/test folder at the home directory of your server and will put files there on insert
});
然后您可以在客户端访问此 collection 以将文件上传到服务器到 ~test/ 目录:
files.insert(new File(['Test file contents'], 'my_test_file'));
然后你可以像这样列出服务器上的文件:
files.find(); // returns [ { createdByTransform: true,
_id: 't6NoXZZdx6hmJDEQh',
original:
{ name: 'my_test_file',
updatedAt: (Date)
size: (N),
type: '' },
uploadedAt: (Date),
copies: { my_files: [Object] },
collectionName: 'my_files'
}
copies
object 似乎包含创建文件的实际名称,例如
files.findOne().copies
{
"my_files" : {
"name" : "testy1",
"type" : "",
"size" : 6,
"key" : "my_files-t6NoXZZdx6hmJDEQh-my_test_file", // This is the name of the file on the server at ~/test/
"updatedAt" : ISODate("2015-03-29T16:53:33Z"),
"createdAt" : ISODate("2015-03-29T16:53:33Z")
}
}
这种方法的问题是它只跟踪通过 Collection;如果你手动添加一些东西到 ~/test 目录,它不会被镜像到 Collection。例如,如果在服务器上我 运行 类似...
mkfile 1k ~/test/my_files-aaaaaaaaaa-manually-created
然后我在collection里找,没有:
files.findOne({"original.name": {$regex: ".*manually.*"}}) // returns undefined
如果您只想在服务器上获得一个简单的文件列表,您可以考虑只运行宁一个ls
。来自 https://gentlenode.com/journal/meteor-14-execute-a-unix-command/33 you can execute any arbitrary UNIX command using Node's child_process.exec()
. You can access the app root directory with process.env.PWD
(from this question)。所以最后如果你想列出你的 public 目录中的所有文件,例如,你可以这样做:
exec = Npm.require('child_process').exec;
console.log("This is the root dir:");
console.log(process.env.PWD); // running from localhost returns: /Users/me/meteor_apps/test
child = exec('ls -la ' + process.env.PWD + '/public', function(error, stdout, stderr) {
// Fill in this callback with whatever you actually want to do with the information
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null) {
console.log('exec error: ' + error);
}
});
这将必须 运行 在服务器上,所以如果您想要客户端上的信息,您必须将它放在一个方法中。这也很不安全,具体取决于您构建它的方式,因此您需要考虑如何阻止人们列出您服务器上的所有文件和文件夹,或者更糟——运行宁任意执行。
您选择哪种方法可能取决于您真正想要完成的目标。
另一种方法是添加 shelljs npm 模块。
要添加 npm 模块,请参阅:https://github.com/meteorhacks/npm
然后你只需要做类似的事情:
var shell = Meteor.npmRequire('shelljs');
var list = shell.ls('/yourfolder');
Shelljs 文档:
https://github.com/arturadib/shelljs
如何使用 Meteor
列出文件夹中的所有文件。我的应用程序上安装了 FS
collection 和 cfs:filesystem
。我没有在文档中找到它。
简短的回答是 FS.Collection 创建了一个 Mongo collection,您可以像对待其他任何东西一样对待它,即,您可以使用 find()
列出条目。
长答案...
使用 cfs:filesystem,您可以创建一个 mongo 数据库来镜像服务器上的给定文件夹,如下所示:
// in lib/files.js
files = new FS.Collection("my_files", {
stores: [new FS.Store.FileSystem("my_files", {"~/test"})] // creates a ~/test folder at the home directory of your server and will put files there on insert
});
然后您可以在客户端访问此 collection 以将文件上传到服务器到 ~test/ 目录:
files.insert(new File(['Test file contents'], 'my_test_file'));
然后你可以像这样列出服务器上的文件:
files.find(); // returns [ { createdByTransform: true,
_id: 't6NoXZZdx6hmJDEQh',
original:
{ name: 'my_test_file',
updatedAt: (Date)
size: (N),
type: '' },
uploadedAt: (Date),
copies: { my_files: [Object] },
collectionName: 'my_files'
}
copies
object 似乎包含创建文件的实际名称,例如
files.findOne().copies
{
"my_files" : {
"name" : "testy1",
"type" : "",
"size" : 6,
"key" : "my_files-t6NoXZZdx6hmJDEQh-my_test_file", // This is the name of the file on the server at ~/test/
"updatedAt" : ISODate("2015-03-29T16:53:33Z"),
"createdAt" : ISODate("2015-03-29T16:53:33Z")
}
}
这种方法的问题是它只跟踪通过 Collection;如果你手动添加一些东西到 ~/test 目录,它不会被镜像到 Collection。例如,如果在服务器上我 运行 类似...
mkfile 1k ~/test/my_files-aaaaaaaaaa-manually-created
然后我在collection里找,没有:
files.findOne({"original.name": {$regex: ".*manually.*"}}) // returns undefined
如果您只想在服务器上获得一个简单的文件列表,您可以考虑只运行宁一个ls
。来自 https://gentlenode.com/journal/meteor-14-execute-a-unix-command/33 you can execute any arbitrary UNIX command using Node's child_process.exec()
. You can access the app root directory with process.env.PWD
(from this question)。所以最后如果你想列出你的 public 目录中的所有文件,例如,你可以这样做:
exec = Npm.require('child_process').exec;
console.log("This is the root dir:");
console.log(process.env.PWD); // running from localhost returns: /Users/me/meteor_apps/test
child = exec('ls -la ' + process.env.PWD + '/public', function(error, stdout, stderr) {
// Fill in this callback with whatever you actually want to do with the information
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null) {
console.log('exec error: ' + error);
}
});
这将必须 运行 在服务器上,所以如果您想要客户端上的信息,您必须将它放在一个方法中。这也很不安全,具体取决于您构建它的方式,因此您需要考虑如何阻止人们列出您服务器上的所有文件和文件夹,或者更糟——运行宁任意执行。
您选择哪种方法可能取决于您真正想要完成的目标。
另一种方法是添加 shelljs npm 模块。
要添加 npm 模块,请参阅:https://github.com/meteorhacks/npm
然后你只需要做类似的事情:
var shell = Meteor.npmRequire('shelljs');
var list = shell.ls('/yourfolder');
Shelljs 文档: https://github.com/arturadib/shelljs