如何在 CollectionFS 中插入图像?
How to insert an image in CollectionFS?
我正在尝试使用 meteor 中的 CollectionFS 创建图像集合。我使用了 https://github.com/CollectionFS/Meteor-CollectionFS/wiki/Insert-One-File-From-a-Remote-URL
中的以下代码
var url='data/2.jpg';
var newFile = new FS.File();
newFile.attachData(url, function (error) {
if (error) throw error;
newFile.name("testImage.jpg");
Images.insert(newFile, function (error, fileObj) {});
});
以上代码写在'js/server.js'文件中的启动函数中&它所指的图像是'js/data/2.jpg'.
但它似乎不起作用并抛出此错误:
Error: ENOENT, stat 'C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data.jpg'
错误ENOENT
是“Error NO ENTry”的缩写。您收到此错误是因为文件 2.jpg
无法访问或不存在于目录 C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data\
.
中
如果要从远程 URL 插入文件,您需要提供可访问的 URL,例如:
Images = new FS.Collection("images", {
stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
});
if (Meteor.isServer) {
Meteor.startup(function () {
var url = 'http://www.panderson.me/images/lena.jpg';
var newFile = new FS.File();
newFile.attachData(url, function (error) {
if (error) throw error;
newFile.name("lena.jpg");
Images.insert(newFile, function (error, fileObj) {
console.log(error);
console.log(fileObj);
});
});
});
}
我假设您不想从远程 URL 插入文件。如果是这种情况,请将文件放在 private
目录中并将变量 url
更改为:"assets/app/lena.jpg"
.
Images = new FS.Collection("images", {
stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
});
if (Meteor.isServer) {
Meteor.startup(function () {
var url = "assets/app/lena.jpg";
var newFile = new FS.File();
newFile.attachData(url, function (error) {
if (error) throw error;
newFile.name("lena.jpg");
Images.insert(newFile, function (error, fileObj) {
console.log(error);
console.log(fileObj);
});
});
});
}
我正在尝试使用 meteor 中的 CollectionFS 创建图像集合。我使用了 https://github.com/CollectionFS/Meteor-CollectionFS/wiki/Insert-One-File-From-a-Remote-URL
中的以下代码 var url='data/2.jpg';
var newFile = new FS.File();
newFile.attachData(url, function (error) {
if (error) throw error;
newFile.name("testImage.jpg");
Images.insert(newFile, function (error, fileObj) {});
});
以上代码写在'js/server.js'文件中的启动函数中&它所指的图像是'js/data/2.jpg'.
但它似乎不起作用并抛出此错误:
Error: ENOENT, stat 'C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data.jpg'
错误ENOENT
是“Error NO ENTry”的缩写。您收到此错误是因为文件 2.jpg
无法访问或不存在于目录 C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data\
.
如果要从远程 URL 插入文件,您需要提供可访问的 URL,例如:
Images = new FS.Collection("images", {
stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
});
if (Meteor.isServer) {
Meteor.startup(function () {
var url = 'http://www.panderson.me/images/lena.jpg';
var newFile = new FS.File();
newFile.attachData(url, function (error) {
if (error) throw error;
newFile.name("lena.jpg");
Images.insert(newFile, function (error, fileObj) {
console.log(error);
console.log(fileObj);
});
});
});
}
我假设您不想从远程 URL 插入文件。如果是这种情况,请将文件放在 private
目录中并将变量 url
更改为:"assets/app/lena.jpg"
.
Images = new FS.Collection("images", {
stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
});
if (Meteor.isServer) {
Meteor.startup(function () {
var url = "assets/app/lena.jpg";
var newFile = new FS.File();
newFile.attachData(url, function (error) {
if (error) throw error;
newFile.name("lena.jpg");
Images.insert(newFile, function (error, fileObj) {
console.log(error);
console.log(fileObj);
});
});
});
}