2014-10-01 40 views
7

我正在使用collectionFS進行文件上傳,我的代碼如下圖所示。記錄的用戶可以將圖像插入到圖像集合中,我也可以看到該文件上傳到服務器上。圖像鏈接和下載按鈕顯示之前刪除不安全的軟件包能夠看到圖像和下載。從項目中刪除不安全的軟件包之後。圖像不顯示也下載不起作用(可以檢索圖像名稱和URL),接收訪問被拒絕403錯誤。我真正想要的是,簽名用戶可以插入文件到服務器,每個人都可以看到圖像,也可以下載文件。我寫了允許規則,還發布和訂閱。這裏有什麼問題?
js文件CollectionFS訪問被拒絕403錯誤#Meteor JS

if (Meteor.isClient) { 
    Template.myForm.events({ 
     'change .myFileInput': function(event, template) { 
     FS.Utility.eachFile(event, function(file) { 
      var fsFile = new FS.File(event.target.files[0]); 
      fsFile.owner = Meteor.userId(); 
      Images.insert(file, function (err, fileObj) { 
      //If !err, we have inserted new doc with ID fileObj._id, and 
      //kicked off the data upload using HTTP 
      }); 
     }); 
     } 
    }); 


    Template.imageView.helpers({ 
     images: function() { 
     return Images.find(); // Where Images is an FS.Collection instance 
     } 
    }); 
    Meteor.subscribe('images'); 
    } 





    if (Meteor.isServer) { 
     Meteor.startup(function() { 
      // code to run on server at startup 
     }); 
     Meteor.publish('images', function(){ 
      return Images.find(); 
     }); 
     } 


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

    Images.allow({ 
    insert: function(userId, doc){ 
     return !!userId; 
    }, 
    update: function(userId, doc){ 
     return !!userId; 
    }, 
    remove: function(userId, doc){ 
     return false; 
    } 
    }); 

HTML文件

<head> 
    <title>uploader</title> 
</head> 

<body> 
    {{> loginButtons}} 
    {{> imageView}} 
    {{>myForm}} 
</body> 

<template name="imageView"> 
    <div class="imageView"> 
     {{#each images}} 
     <div> 
      <a href="{{this.url}}" target="_blank"><img src="{{this.url}}" alt="" class="thumbnail" />{{this.url}}</a><br/> 
      <strong>{{this.name}}</strong> <a href="{{this.url download=true}}" class="btn btn-primary">Download</a> 
     </div> 
     {{/each}} 
    </div> 
</template> 

<template name="myForm"> 
    <p> 
     Please specify a file, or a set of files:<br> 
     <input type="file" name="datafile" class="myFileInput"> 
    </p> 
</template> 

回答

18

如果你有安全感和自動發佈關閉,您通過訂閱訪問您的文件,我相信,你只需要下載變種在你允許的散列。

Uploads.allow({ 
    insert:function(userId,project){ 
    return true; 
    }, 
    update:function(userId,project,fields,modifier){ 
    return true; 
    }, 
    remove:function(userId,project){ 
    return true; 
    }, 
    download:function(){ 
    return true; 
    } 
}); 
+0

謝謝你的回答,我其實已經想通了。順便說一句,我看了你的YouTube流視頻MeteorJS,這是非常好的教程,分頁教程是非常有用的,我用它在我的項目上,再次感謝:) – zevsuld 2014-12-23 07:30:03

+0

我剛剛遇到了同樣的問題。感謝修復! – CodeChimp 2015-09-21 17:37:13