Anders Kitson
Anders Kitson

Reputation: 1545

Trying to add a img upload to my form post in meteor

I am trying to upload images to my public folder in meteor

I am using code from this gist

This is the code that is giving me the problem it is in the client folder but I am getting a undefined error

JS

Template.postSubmit.events({
  'change input': function(ev) {
    _.each(ev.srcElement.files, function(file) {
      Meteor.saveFile(file, file.name);
    });
  }
});

TEMPLATE

<template name="postSubmit">
 <form>

    <input type="file" />

    <div>
        <div>
            <input type="submit" value="Submit" />
        </div>
    </div>

</template>

ERROR

Uncaught TypeError: Cannot read property 'files' of undefined upload.js?574ce0cd76371392e26467130b3b109e7fc0d6b5:3
Template.postSubmit.events.change input upload.js?574ce0cd76371392e26467130b3b109e7fc0d6b5:3
(anonymous function) base.js:317
_assign.nonreactive deps.js:363
wrappedHandler base.js:308
(anonymous function) domrange.js:875
jQuery.event.dispatch jquery.js:4624
elemData.handle

There is more code, but I cant get even the first step going, so Id like to figure out whats going on here thanks.

Upvotes: 0

Views: 203

Answers (2)

jood
jood

Reputation: 2397

@Aram is right, but you also need to change it in the Template event. srcElement is for IE, whereas target is for all the others browsers

Upvotes: 0

Aram
Aram

Reputation: 379

change this part in the client side saveFile function:

 fileReader.onload = function(file) {
   Meteor.call('saveFile', file.srcElement.result, name, path, encoding, callback);
 }

to this:

 fileReader.onload = function(file) {
   Meteor.call('saveFile', file.target.result, name, path, encoding, callback);
 }

basically you need to use file.target.result instead of file.srcElement.result

Upvotes: 1

Related Questions