4

To get the value of <input type="file" name="upload"> with jQuery, you could use $('input').val().

However, using the same method to get the value of <input type="file" name="upload[]" multiple> (multiple-file input) only returns one file, not all of them.

Is there a different way to do this?

4
  • Did you find a solution for this? The answers are not helpful at all. Commented Mar 18, 2013 at 22:57
  • 3
    @Alqin Yep. Check out stackoverflow.com/questions/14035530/… Commented Mar 18, 2013 at 23:11
  • but that link links to a pure-js solution, not jquery Commented Oct 8, 2013 at 7:48
  • @dsdsdsdsd That is true. But that's the closest a solution came to the question Commented Oct 8, 2013 at 22:16

3 Answers 3

2

try console into

document.forms[index].elements[index].files

cache the values an loop through them

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

Comments

1

I used some code from another post...

$('input[type=file]').val()

..and added the ID of each specific file input field. The code below gets the path from the input, pulls only the filename from it and shows that value in a different field for two different file upload fields. If you had a ton of them, you could create a loop based on this code:

$('#File1').change(function(){ // when you leave the File1 form field
  var filename1 = $('input[type=file][id=File1]').val().split('\\').pop(); // get just the filename
  $('#DL_Filename1').val(filename1); // and place it in the DL_Filename1 box
}); // end change

$('#File2').change(function(){ // when you leave the File2 form field
  var filename2 = $('input[type=file][id=File2]').val().split('\\').pop(); // get just the filename
  $('#DL_Filename2').val(filename2); // and place it in the DL_Filename2 box
}); // end change

1 Comment

Maybe you could provide attribution for the "code from another post..." Thanks.
0

That's because, when you use$('input').val() it returns the value of only the first of those inputs. You need to loop over to get the values of all of them. See the below code:

 var $uploads = $('input');

 $uploads.each(function() {

     console.log($(this).val());

 });

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.