【发布时间】:2015-06-02 16:24:26
【问题描述】:
我对meteor 将静态文件保存在哪里(/public 文件夹中的文件)有点困惑;
我有上传文件的方法:
saveFile: function(blob, name, path, encoding) {
if (!Meteor.isServer) return;
var fs = Npm.require('fs'),
chroot = Meteor.chroot || 'public';
//path = cleanPath(path);
name = cleanName(name || 'file');
encoding = encoding || 'binary';
path = chroot + (path ? '/' + path + '/' : '/');
var basePath = process.env.PWD;
var fullPath = basePath + '/' + path + name;
var dirs = (path.split('/'));
var currentCreatedPath = basePath;
//create needed folders
dirs.forEach(function(dir, index) {
if (dir.length > 0) {
currentCreatedPath = currentCreatedPath + '/' + dir;
if (!fs.existsSync(currentCreatedPath)){
fs.mkdirSync(currentCreatedPath);
}
}
});
fs.writeFile(fullPath, blob, encoding, function(err) {
if (err) console.log(err); //throw (new Meteor.Error(500, 'Failed to save file.', err));
else console.log('The file ' + fullPath + 'has been saved');
});
function cleanPath(str) {
if (str) return str.replace(/\.\./g,'').replace(/\/+/g,'').replace(/^\/+/,'').replace(/\/+$/,'');
}
function cleanName(str) {
return str.replace(/\.\./g,'').replace(/\//g,'');
}
return true;
}
然后在模板中我到达这样的文件:
{{#each this.files}}
<li>
<a href="/{{path}}" target="_blank">
{{title}}
</a>
</li>
{{/each}}
路径是/public/folder/to/file/file.ext
这在本地很有效;但是一旦部署,它就无法找到上传的文件; meteor 将文件保存在部署项目的哪个文件夹中?
【问题讨论】:
-
/public目录不用于存储上传的文件。我会推荐使用 CollectionFS 而不是原生的 Npm fs。在这种情况下,使用文件系统存储适配器,您的文件将默认上传到您的PROJECT/cfs/files/,您可以在存储创建期间指定路径stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]CollectionFS 提供 url 帮助程序,然后获取指向这些文件的链接。另一种方法是使用基于 PHP 的上传服务器 -
感谢您的建议;我试过 CollectionFS;它在本地工作,但一旦部署到我的服务器,应用程序就会在启动时崩溃;现在我不知道为什么
-
这很可能是因为您正在使用 fs 存储并且
/cfs/files/_temporary文件夹不可写。要解决此问题,运行您的流星应用程序的用户必须具有写入权限。使用meteor-up而不指定自定义路径将使该文件夹位于:/opt/<yourProjectName>/cfs/files/_tempstore/
标签: meteor