首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CollectionFS中插入图像?

如何在CollectionFS中插入图像?
EN

Stack Overflow用户
提问于 2015-11-16 11:43:24
回答 1查看 262关注 0票数 0

我正在尝试使用流星中的CollectionFS创建一个图像集合。我使用了来自https://github.com/CollectionFS/Meteor-CollectionFS/wiki/Insert-One-File-From-a-Remote-URL的以下代码

代码语言:javascript
复制
        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’。

但是它似乎不起作用&抛出这个错误:

代码语言:javascript
复制
Error: ENOENT, stat 'C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data\2.jpg'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-16 17:20:03

error ENOENT是“Error NO ENTry”的缩写。您收到此错误是因为文件2.jpg无法访问,或者在目录C:\Users\[username]\WebstormProjects\test\.meteor\local\build\programs\server\data\中不存在。

如果要从远程URL插入文件,则需要提供可访问的URL,例如:

代码语言:javascript
复制
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"

代码语言:javascript
复制
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);
            });
        });
    });
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33734556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档