0

i was trying to upload a file via $.ajax call using jQuery.. and formData

in order to append the file to the formData i did the following:

var fd = new formData();
fd.append($('#myFileInput));

that failed

then i tried :

var fd = new formData();
fd.append($('#myFileInput).files[0]);

and again it failed then i tried:

var fd = new formData();
fd.append($('#myFileInput')[0].files[0]);

and that really worked and i could send my file to the server..

my question is:

why should I use the [0] next to the jquery object representing my file input element.. ?? notice that i didn't use the "multiple" attribute for the file input

3
  • 1
    FYI, the other lines were missing the closing '. Commented Mar 11, 2014 at 7:41
  • sorry for that typo... but it wasn't missing the closing in the origional code Commented Mar 11, 2014 at 7:47
  • and was it $('myFileInput') or $('.myFileInput') ? Notice the . Commented Mar 11, 2014 at 7:50

1 Answer 1

2

$('selector') returns a jQuery object, and "files" attribute belongs to the DOM object, thus you will need to retrieve the DOM object via $('selector')[0] or $('selector').get(0).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.