2015-06-29 41 views
1

您好,我正在使用CollectionFS上傳和存儲圖像。CollectionFS獲取映像文件的url

上傳和存儲文件成功。我可以從發佈中檢索文件對象。但我無法獲取文件的URL。我需要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…} 

但網址傳回空。

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.userIdthis.userId是不確定的。

回答

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

我希望這有助於

+0

試過指定的方法。使用URL檢索圖像時,我得到403禁止消息。我應該添加任何安全規則嗎? –

+0

我想你可能需要暫停或在圖像被加載到商店時使其處於被動狀態。 – LongWalks