【问题标题】:Get file URL of a FS.File object with Meteor使用 Meteor 获取 FS.File 对象的文件 URL
【发布时间】:2014-08-14 05:28:51
【问题描述】:

我正在使用 Meteor 的 CollectionFS 并尝试显示上传到服务器的图像文件。我在另一个对象中附加了对文件对象的引用,例如:

  Entries.insert({
    title: title,
    caption: caption,
    file: fsFile,
  });

我调用Entries.find({}) 并将其返回到一个模板,我用它来遍历条目。我试过<img src="file.url">,但是不行。

如果我直接调用图像集合 Images.find({}) 并遍历文件,使用文件上下文 this.url 获取它们的 url,它工作正常。是否有类似的方法可以使用 Entires 对象中的引用来实现?

【问题讨论】:

  • 查看这部分文档:github.com/CollectionFS/… fsFile 只是对文件的引用。如果要获取图像的数据,则必须使用 file.getFileRecord() 方法。这仅在图像已订阅时才有效。如果你不使用不安全/自动发布,你必须自己做一些连接。
  • 没错。您需要进行一些连接,但 getFileRecord() 没有提供太多信息。尝试将FS.debug=true 放在您处理提交事件的位置。在浏览器控制台上,您可以看到它创建的链接可能是 /cfs/files/<collectionName>/<fileObj.id>

标签: url collections meteor meteorite


【解决方案1】:

试试这样的:(注意我使用的是下划线包

Template.image_queue.helpers({
    images: function() {
        return _.map(Images.find().fetch(), function(image) {
            return image.url();
        });
    }
});

假设

Images = new FS.Collection("images", {
    stores: [new FS.Store.FileSystem("images", {
        path: "~/uploads"
    })]
});

Images.allow({
    insert: function (userId, party) {
        return true;
    },
    update: function (userId, party) {
        return true;
    },
    remove: function (userId, party) {
        return true;
    },
    download: function (userId, party) {
        return true;
    }
});

【讨论】:

  • 非常感谢!
  • 效果很好,这是我找到的最好的解决方案,并且在文档中真的很想念它,因为普通的 {{this.url}} 解决方案根本不灵活,尤其是在通过另一个集合引用图像时.我发现的唯一缺点是处理程序会为每个图像调用两次。我是这样使用它的:var result = _.map(welcomeImages.find({"_id": imageId}).fetch(), function(image) { return image.url(); }); while 循环遍历主集合,其中 imageId 是对相应图像的引用。
【解决方案2】:

如果您尚未发布和订阅您的 CFS 文件,file.url 将返回 undefined。在这种情况下,您可以通过以下方式获取文件 url:

/cfs/files/{cfs_collection_name}/{fs_file_id}

例如:

<img src="/cfs/files/images/{{ file._id }}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2023-03-24
    • 2017-10-24
    • 2016-01-24
    • 2014-09-29
    • 1970-01-01
    • 2012-08-06
    相关资源
    最近更新 更多