当托管在 meteor.com 上时,从服务器的文件系统读取 CollectionFS 文件
Read CollectionFS file from server's filesystem when hosted on meteor.com
我正在尝试让用户上传一个 txt 文件,然后让他点击一个按钮 "analyze",然后执行一些分析。
我的应用程序在本地运行,我使用 FS.Collection 和文件系统,但是我在部署到 meteor.com 时遇到了几个问题。这是我的 collection:
FS.debug = true;
Uploads = new FS.Collection('uploads', {
stores: [new FS.Store.FileSystem('uploads')]
});
这是我尝试读取上传文件的方式:
var fs = Npm.require('fs');
var readedFile = fs.readFileSync(process.env.PWD+'/.meteor/local/cfs/files/uploads/+file.copies.uploads.key, 'utf-8');
以上在本地有效,但在我部署到 meteor.com 后无效,在调试消息中我看到类似这样的内容:Error: ENOENT, no such file or directory
所以我不知道在部署应用程序时如何读取文件,你会怎么做?或者你认为我应该将应用程序部署到 Amazon EC2?我害怕部署到亚马逊并遇到同样的问题...
这可能是您想要的包:
https://github.com/tomitrescak/meteor-uploads
它也有一个很好的 UI,而且比 FSCollection 麻烦少得多。
使用 http 下载通过 collectionFS 上传的文件的简短示例。
var file = Uploads.findOne({ _id: myId }); // or however you find it
HTTP.get(file.url(),function(err,result){
// this will be async obviously
if ( err ) console.log("Error "+err+" downloading file"+myId);
else {
var content = result.content; // the contents of the file
// now do something with it
}
});
请注意,您必须 meteor add http
才能访问 http 包。
我正在尝试让用户上传一个 txt 文件,然后让他点击一个按钮 "analyze",然后执行一些分析。
我的应用程序在本地运行,我使用 FS.Collection 和文件系统,但是我在部署到 meteor.com 时遇到了几个问题。这是我的 collection:
FS.debug = true;
Uploads = new FS.Collection('uploads', {
stores: [new FS.Store.FileSystem('uploads')]
});
这是我尝试读取上传文件的方式:
var fs = Npm.require('fs');
var readedFile = fs.readFileSync(process.env.PWD+'/.meteor/local/cfs/files/uploads/+file.copies.uploads.key, 'utf-8');
以上在本地有效,但在我部署到 meteor.com 后无效,在调试消息中我看到类似这样的内容:Error: ENOENT, no such file or directory
所以我不知道在部署应用程序时如何读取文件,你会怎么做?或者你认为我应该将应用程序部署到 Amazon EC2?我害怕部署到亚马逊并遇到同样的问题...
这可能是您想要的包:
https://github.com/tomitrescak/meteor-uploads
它也有一个很好的 UI,而且比 FSCollection 麻烦少得多。
使用 http 下载通过 collectionFS 上传的文件的简短示例。
var file = Uploads.findOne({ _id: myId }); // or however you find it
HTTP.get(file.url(),function(err,result){
// this will be async obviously
if ( err ) console.log("Error "+err+" downloading file"+myId);
else {
var content = result.content; // the contents of the file
// now do something with it
}
});
请注意,您必须 meteor add http
才能访问 http 包。