如何使用Meteor处理文件上传?

Dav*_*vid 76 javascript file-upload meteor

使用Meteor处理文件上传的规范方法是什么?

小智 44

我用过http://filepicker.io.他们将上传文件,将其存储到S3中,然后返回文件所在的URL.然后我只是把网址放到数据库中.

  1. 将filepicker脚本输入到客户端文件夹中.

    wget https://api.filepicker.io/v0/filepicker.js
    
    Run Code Online (Sandbox Code Playgroud)
  2. 插入filepicker输入标记

    <input type="filepicker" id="attachment">
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在启动时,初始化它:

    Meteor.startup( function() {
        filepicker.setKey("YOUR FILEPICKER API KEY");
        filepicker.constructWidget(document.getElementById('attachment'));
    });
    
    Run Code Online (Sandbox Code Playgroud)
  4. 附加事件处理程序

    Templates.template.events({
        'change #attachment': function(evt){
            console.log(evt.files);
        }
    });
    
    Run Code Online (Sandbox Code Playgroud)

  • Pfff ..我不打算只花100美元将文件上传到S3. (8认同)
  • Echo @rijk,不知道为什么付费服务是#1投票答案.. https://github.com/VeliovGroup/Meteor-Files或https://themeteorchef.com/recipes/uploading-files-to-amazon -s3 /都是实现开源库的免费解决方案. (6认同)

Har*_*ove 26

对于图像,我使用类似于Dario的方法,除了我不将文件写入磁盘.我将数据作为模型中的字段直接存储在数据库中.这对我有用,因为我只需要支持支持HTML5 File API的浏览器.我只需要简单的图像支持.

Template.myForm.events({
  'submit form': function(e, template) {
    e.preventDefault();
    var file = template.find('input type=["file"]').files[0];
    var reader = new FileReader();
    reader.onload = function(e) {
      // Add it to your model
      model.update(id, { $set: { src: e.target.result }});

      // Update an image on the page with the data
      $(template.find('img')).attr('src', e.target.result);
    }
    reader.readAsDataURL(file);
  }
});
Run Code Online (Sandbox Code Playgroud)


Dar*_*río 19

我刚刚使用Meteor.methods和HTML5 File的API 提出了文件上传的实现.让我知道你的想法.

  • 所以,这些说明效果出奇的好.解决方案比我预期的容易了10倍,代码几乎完美无缺.话虽这么说,解决方案将图像直接上传到node.js本地文件系统.它最初在本地开发机器上运行良好,但在平台即服务(PaaS)提供商(包括Heroku和Nodjitsu)方面存在问题.问题是此解决方案存在文件系统权限问题.因此,此解决方案需要托管您自己的服务器,或者拥有更强大的基础架构,例如Amazon Elasticbeanstalk. (3认同)

Ray*_*nos 17

目前似乎没有办法与HTTP服务器交互或执行与HTTP相关的任何事情.

您可以做的唯一事情是通过Meteor.methods公开的RPC方法与服务器通信,或者直接通过暴露的mongoDB API与mongoDB交谈.

  • 我在这里参加派对有点晚了,但你也可以看看我在为流星建立一个文件上传器的最后几集eventedmind.com.该软件包的流式上传版本将于本周发布.它被称为流星文件. (3认同)

d_i*_*ble 11

有一个新的包:edgee:弹弓.它不会将文件上传到您的流星服务器,但它更好的方式,因为它允许流星服务器专注于其服务流星应用程序的主要目标,而不是处理昂贵的文件传输.

相反,它将文件上传到云存储服务.目前它支持AWS S3和Google Cloud Files,但它也将支持Rackspace Cloud Files以及未来的Cloudinary.

您的流星服务器仅仅充当协调器.

直接VS间接上传

它也是一种用途广泛且重量轻的包装.


Mic*_*oon 7

有一个叫做路由器的气氛包就可以了.

实际上,处理文件上传的最佳方法现在是collectionFS


Raz*_*Raz 7

这是目前最好的解决方案.它使用collectionFS.

meteor add cfs:standard-packages
meteor add cfs:filesystem
Run Code Online (Sandbox Code Playgroud)

客户:

Template.yourTemplate.events({
    'change .your-upload-class': function(event, template) {
        FS.Utility.eachFile(event, function(file) {
            var yourFile = new FS.File(file);
            yourFile.creatorId = Meteor.userId(); // add custom data
            YourFileCollection.insert(yourFile, function (err, fileObj) {
                if (!err) {
                   // do callback stuff
                }
            });
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

服务器:

YourFileCollection = new FS.Collection("yourFileCollection", {
    stores: [new FS.Store.FileSystem("yourFileCollection", {path: "~/meteor_uploads"})]
});
YourFileCollection.allow({
    insert: function (userId, doc) {
        return !!userId;
    },
    update: function (userId, doc) {
        return doc.creatorId == userId
    },
    download: function (userId, doc) {
        return doc.creatorId == userId
    }
});
Run Code Online (Sandbox Code Playgroud)

模板:

<template name="yourTemplate">
    <input class="your-upload-class" type="file">
</template>
Run Code Online (Sandbox Code Playgroud)