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?

Voriki
  • 1,617
  • 2
  • 19
  • 43

3 Answers3

2

try console into

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

cache the values an loop through them

Philip
  • 4,592
  • 2
  • 20
  • 28
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
    Maybe you could provide attribution for the "code from another post..." Thanks. – Kev Sep 22 '12 at 22:46
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());

 });
Sandeep
  • 819
  • 10
  • 16