如何使用 cfs:filesystem 和 collectionfs 在 Meteor 中保存到 /public?
How to save to /public in Meteor with cfs:filesystem and collectionfs?
/lib/collections/images.js
var imageStore = new FS.Store.FileSystem("images", {
// what should the path be if I want to save to /public/assets?
// this does not work
path: "/assets/images/",
maxTries: 1
});
Images = new FS.Collection("images", {
stores: [imageStore]
});
Images.deny({
insert: function() {
return false;
},
update: function() {
return false;
},
remove: function() {
return false;
},
download: function() {
return false;
}
});
Images.allow({
insert: function() {
return true;
},
update: function() {
return true;
},
remove: function() {
return true;
},
download: function() {
return true;
}
});
/client/test.html
<template name="test">
<input type="file" name="myFileInput" class="myFileInput">
</template>
/client/test.js
Template.test.events({
'change .myFileInput': function(event, template) {
FS.Utility.eachFile(event, function(file) {
Images.insert(file, function (err, fileObj) {
if (err){
// handle error
} else {
// handle success
}
});
});
},
});
对于路径,如果我使用:
path: "/public/assets/images",
错误:EACCES,权限被拒绝'/public'
path: "/assets/images",
错误:EACCES,权限被拒绝“/assets”
path: "~/assets/images",
这有效,但它会将图像保存到我的 Linux 机器上的 /home/assets/images
。该路径与 Meteor 项目完全无关。
/
不代表您网站的根目录。 /
代表您系统的根目录。流星应用程序以您的用户身份运行。
您需要做的是使用相对路径。 collectionFS fs.write
操作可能不是从应用程序根目录完成的。因此,您也可以使用 path: process.env.PWD + '/public/assets/images'
您可以使用
var homeDir = process.env.HOMEPATH;
tmpDir: homeDir + '/uploads/tmp'
我已经使用 meteor-root 包解决了这个问题:
https://atmospherejs.com/ostrio/meteor-root
Files = new FS.Collection("files", {
stores: [new FS.Store.FileSystem("images", {path: Meteor.absolutePath + '/public/uploads'})]
});
所以现在它将文件存储在 app/public/uploads/
希望对您有所帮助!
/lib/collections/images.js
var imageStore = new FS.Store.FileSystem("images", {
// what should the path be if I want to save to /public/assets?
// this does not work
path: "/assets/images/",
maxTries: 1
});
Images = new FS.Collection("images", {
stores: [imageStore]
});
Images.deny({
insert: function() {
return false;
},
update: function() {
return false;
},
remove: function() {
return false;
},
download: function() {
return false;
}
});
Images.allow({
insert: function() {
return true;
},
update: function() {
return true;
},
remove: function() {
return true;
},
download: function() {
return true;
}
});
/client/test.html
<template name="test">
<input type="file" name="myFileInput" class="myFileInput">
</template>
/client/test.js
Template.test.events({
'change .myFileInput': function(event, template) {
FS.Utility.eachFile(event, function(file) {
Images.insert(file, function (err, fileObj) {
if (err){
// handle error
} else {
// handle success
}
});
});
},
});
对于路径,如果我使用:
path: "/public/assets/images",
错误:EACCES,权限被拒绝'/public'
path: "/assets/images",
错误:EACCES,权限被拒绝“/assets”
path: "~/assets/images",
这有效,但它会将图像保存到我的 Linux 机器上的 /home/assets/images
。该路径与 Meteor 项目完全无关。
/
不代表您网站的根目录。 /
代表您系统的根目录。流星应用程序以您的用户身份运行。
您需要做的是使用相对路径。 collectionFS fs.write
操作可能不是从应用程序根目录完成的。因此,您也可以使用 path: process.env.PWD + '/public/assets/images'
您可以使用
var homeDir = process.env.HOMEPATH;
tmpDir: homeDir + '/uploads/tmp'
我已经使用 meteor-root 包解决了这个问题: https://atmospherejs.com/ostrio/meteor-root
Files = new FS.Collection("files", {
stores: [new FS.Store.FileSystem("images", {path: Meteor.absolutePath + '/public/uploads'})]
});
所以现在它将文件存储在 app/public/uploads/
希望对您有所帮助!