0

I'm trying to create a collection of images using the CollectionFS in meteor. I used the following code from 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) {});
        });        

The above code is written in the startup function in the 'js/server.js' file & the image it is referring to is 'js/data/2.jpg'.

But it doesn't seem to work & throws the this error:

Error: ENOENT, stat 'C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data\2.jpg'
4

1 回答 1

0

该错误ENOENT是“<strong>Error NO ENT ry”的缩写。您收到此错误是因为该文件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);
            });
        });
    });
}

于 2015-11-16T17:20:03.593 回答