【问题标题】:collectionfs generate download urlcollectionfs 生成下载地址
【发布时间】:2013-09-20 17:34:49
【问题描述】:

我正在做一个使用流星和collectionfs的项目。

我将文件上传到 collectionfs 并有一个文件处理程序。我可以使用 {{cfsFileUrl "defaultFilehandler"}}

Handlebar Helper 显示保存图像的 url,但我无法从该 URL 下载图像。

当我将其复制到浏览器中时:

localhost:3000/cfs/contacts/Nj3WzrBKhqd9Mc9NP_defaultHandler.png

meteor 将我引导到流星页面(就好像我写了 localhost:3000 一样)

最终我想实现两件事:

第一个

使用 html 标签显示图像:

<img src=??? alt="your image" />

第二 我想确保允许用户看到这张图片。

拥有“下载网址”对我来说不够安全。

为了明白这一点,我从 collectionFS 完成了正常的教程:

客户端js

ContactsFS = new CollectionFS('contacts', { autopublish: false });

Deps.autorun(function() {
    Meteor.subscribe('myContactsFiles');
});

Template.queueControl.events({
    'change .fileUploader': function (e) {
        var files = e.target.files;
        for (var i = 0, f; f = files[i]; i++) {
            ContactsFS.storeFile(f);
        }
    }
});

服务器 js

ContactsFS = new CollectionFS('contacts', { autopublish: false });

ContactsFS.allow({
    insert: function(userId, file) { 
        console.log('user'+userId+"file"+JSON.stringify(file));
        console.log("WILL SAVE:"+userId && file.owner === userId );
        return userId && file.owner === userId; 
    },
    update: function(userId, files, fields, modifier) {
        return _.all(files, function (file) {
            return (userId == file.owner);
        });  //EO iterate through files
    },
    remove: function(userId, files) { return false; }
});

Meteor.publish('myContactsFiles', function() {
    if (this.userId) {
        return ContactsFS.find({ owner: this.userId }, { limit: 30 });
    }
});


ContactsFS.fileHandlers({
  default1: function(options) { // Options contains blob and fileRecord — same is expected in return if should be saved on filesytem, can be modified
    return { blob: options.blob, fileRecord: options.fileRecord }; // if no blob then save result in fileHandle (added createdAt)
  }});

【问题讨论】:

  • 您好,我在尝试显示图像时遇到了问题。它显示了一个带有此错误的损坏的图像图标,“资源解释为图像但使用 MIME 类型文本/html 传输:”。你也遇到过同样的问题吗?

标签: html image meteor


【解决方案1】:

答案 1:

您可以使用更新的 0.3.3+ 版本的 collectionFS 公开地使这些文件可访问或查看它们。

<img src="{{cfsFileUrl 'default1'}}">

其中default1 是您在ContactsFS.fileHandlers 中定义的处理函数的名称

答案 2:

到目前为止,collectionFS 还没有内置的安全解决方案,它正在制作中。

【讨论】:

  • 按照您的建议后,图像图标显示,但由于此错误“资源解释为图像但使用 MIME 类型文本/html 传输:”而损坏。知道我应该如何解决这个问题吗??
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
相关资源
最近更新 更多