【问题标题】:CollectionFS getting url of image fileCollectionFS 获取图像文件的 url
【发布时间】:2015-06-29 09:23:17
【问题描述】:

您好,我正在使用 CollectionFS 上传和存储图片。

上传和存储文件成功。我可以从出版物中检索文件对象。但我无法获取文件的 URL。我需要img 标签的网址

以下是我声明集合的方式。

Images = new FS.Collection("images", {
    stores: [new FS.Store.GridFS("images")]
});

在浏览器控制台中,以下代码返回 FS.File 对象

NewsImages.find().fetch()[0]
FS.File {createdByTransform: true, _id: "3kqLZRdLD33dKir2M", original: Object, chunkSize: 2097152, chunkCount: 0…}

但是 url 返回 null。

NewsImages.find().fetch()[0].url()
null

生成 URL 是否需要额外的工作?

更多详情

我正在使用以下允许规则。

NewsImages.allow({
    //more others
    download: function(){
        if(Meteor.userId()){
            return true;
        }else{
            return false;
        }
    }
});

我收到以下异常。

Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.

我将Meteor.userId() 替换为this.userId,但this.userId 未定义。

【问题讨论】:

    标签: meteor collectionfs


    【解决方案1】:

    在保存图像时使用回调,然后将图像 id 放入另一个 收藏。粗略的伪代码...

    Images.insert(yourFsFile, function (error, fileObj) {
        if (error) {
          // cry!
        }
    
        _.extend(yourItem, {imageId: fileObj._id});
    
        Meteor.call('otherCollectioInsert', yourItem, function(error, result) {
          if (error) {
            // cry again!
          }
          // should have saved the image id so you can use the url
          // http://www.you.com/cfs/files/images/the_id_you_saved
        });
      });
    

    希望对你有帮助

    【讨论】:

    • 试过指定的方法。使用 URL 检索图像时收到 403 禁止消息。我应该添加任何安全规则吗?
    • 我认为您可能需要超时或在图像加载到商店时使其反应。
    猜你喜欢
    • 1970-01-01
    • 2015-09-08
    • 2023-03-22
    • 2016-01-16
    • 1970-01-01
    • 2016-09-16
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多