【问题标题】:How to insert an image in CollectionFS?如何在 CollectionFS 中插入图像?
【发布时间】:2015-11-17 01:42:21
【问题描述】:

我正在尝试使用流星中的 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\2.jpg'

【问题讨论】:

    标签: javascript mongodb meteor collectionfs


    【解决方案1】:

    错误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);
                });
            });
        });
    }

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 1970-01-01
      • 2015-12-12
      • 2015-09-29
      • 2019-11-08
      • 2018-05-24
      • 1970-01-01
      • 2014-05-27
      相关资源
      最近更新 更多