Bobby Bobby - 23 days ago 7
HTML Question

Display all selected files

I have an input field that allows me to selected multiples files (images in my case), the problem is that when I select more that one file it'll only show one in google chrome. However, it works just fine on IE, so not sure where the problem is coming from. any idea?

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<label class="btn btn-primary" for="my-file-selector">
<input id="my-file-selector" type="file" name="files[]" style="display:none;" multiple onchange="$('#upload-file-info').html($(this).val());">Browse</label>
<span class='label label-info' id="upload-file-info"></span>
<div style="float:right;">
<label class="btn btn-primary" for="my-file-selector2">
<input id="my-file-selector2" type="Submit" style="display:none;" name="search">Import</label>
</div>
</form>

Answer

This has nothing to do with your browser.

$(this).val() // This only contains the last file name

$(this).prop('files'); does contain all the uploaded files as a FileList object.

Loop the files property and put all the names in your #upload-file-info

$('#my-file-selector').on('change', function(e){
  $('#upload-file-info').html(""); //clear the info before appending
  var files = $(this).prop('files');
  for (var i = 0; i < files.length; i++) {
  	$('#upload-file-info').append(files[i].name);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form  action="" method="post" enctype="multipart/form-data">
    <label class="btn btn-primary" for="my-file-selector">
    <input id="my-file-selector" type="file"  name="files[]" style="display:none;" multiple>Browse</label>
    <span class='label label-info' id="upload-file-info"></span>
    <div style="float:right;">
        <label class="btn btn-primary" for="my-file-selector2">
        <input id="my-file-selector2" type="Submit" style="display:none;" name="search">Import</label>
    </div>
</form>

More information about File in Browser specs:

FileAPI reference
files property reference