最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论