javascript - Meteor event.target.files is undefined? - Stack Overflow

Following code for the template:<form><input type="text" name="title" plac

Following code for the template:

<form>
    <input type="text" name="title" placeholder="Title" />
    <input type="file" id="file" accept="image/*" />
    <input type="submit" value="Upload" />
</form>  

And then in my events for that template:

'submit form': function (event, template)
{
    event.preventDefault();
    var file = event.target.files;
    var title = event.target.title.value;
    var image = Images.insert(file[0], function (err, fileObj)
    {

    });
    Bla.insert({name: title, userId: '123123', image: image._id});
}

Now I am getting the error: Uncaught TypeError: Cannot read property '0' of undefined

So event.target.files is undefined, but I don't get why.

When I just use the file input and then a change event handler on that + jQuery get to get the file, it works.

Following code for the template:

<form>
    <input type="text" name="title" placeholder="Title" />
    <input type="file" id="file" accept="image/*" />
    <input type="submit" value="Upload" />
</form>  

And then in my events for that template:

'submit form': function (event, template)
{
    event.preventDefault();
    var file = event.target.files;
    var title = event.target.title.value;
    var image = Images.insert(file[0], function (err, fileObj)
    {

    });
    Bla.insert({name: title, userId: '123123', image: image._id});
}

Now I am getting the error: Uncaught TypeError: Cannot read property '0' of undefined

So event.target.files is undefined, but I don't get why.

When I just use the file input and then a change event handler on that + jQuery get to get the file, it works.

Share Improve this question asked Jan 15, 2016 at 13:36 Frederik WitteFrederik Witte 1,3852 gold badges16 silver badges37 bronze badges 3
  • 1 console.log your whole event and look through that, you'll find out where your files are stored in the event. – Faysal Ahmed Commented Jan 15, 2016 at 13:48
  • Oke I am stupid. I had to do event.target.[name-of-field].files... But shouldn't event.target.files work, too? Thank you. If you make an answer I will accept it – Frederik Witte Commented Jan 15, 2016 at 13:56
  • there's no reason of feeling stupid. I apologize to you if my earlier ment made you feel stupid. JQuery event and meteor events are not the same. they do handle things differently. – Faysal Ahmed Commented Jan 15, 2016 at 13:58
Add a ment  | 

1 Answer 1

Reset to default 4

Try to console.log your event and you'll get the location of your files where it's stored in the event. it'll be similar like this

event.target.[name-of-field].files

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745141212a4613438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信