How would one handle a file upload with Meteor?

后端 未结 12 1446
不知归路
不知归路 2020-12-07 07:56

What would be the canonical way to handle a file upload with Meteor?

12条回答
  •  粉色の甜心
    2020-12-07 08:14

    For images, I use a method similar to Dario's except I don't write the file to disk. I store the data directly in the database as a field on the model. This works for me because I only need to support browsers that support the HTML5 File API. And I only need simple image support.

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

提交回复
热议问题