Frederik Witte
Frederik Witte

Reputation: 1345

Meteor event.target.files is undefined?

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.

Upvotes: 1

Views: 7555

Answers (1)

Faysal Ahmed
Faysal Ahmed

Reputation: 1542

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

Upvotes: 4

Related Questions