I have the following form and div and jQuery. When selecting a file to be uploaded, the name of the file (with it's extension) will output to the div, however, it doesn't work for multiple files. When selecting multiple files, only the name of the first file is output. I looked all over for a solution for multiple files, and could not find it.
<form id="myform" action="" method="post" enctype="multipart/form-data">
<input type="file" multiple name="files[]" />
<input type="submit" name="submit" value="Submit">
</form>
<div class="filename"></div>
$("input:file").change(function (){
var fileName = $(this).val();
$(".filename").html(fileName);
});
So remember, getting the list of file names (with their extensions) should happen as soon as files are selected, not after the form is being submitted.
EDIT:
On top of what i said below, another thing that happens is that when a single file is selected, the name of that file shows up next to the browse button, but when multiple files are selected, it doesn't show the name of each file, it just says "# files selected." for firefox and "# files" for chrome. i went to see what IE does and that one is different. it puts the path of the file or files in the text field next to browse button, so i guess that's what the other question and answer was all about. i tried all of these and none work, and even if one of them did, that seems to be an internet explorer only thing. i need a cross browser solution.
var fileName = $(this).val().split('\\').pop();
var fileName = $(this).val().split(',').pop();
var fileName = $(this).val().split(', ').pop();
So to recap, when selecting multiple files, the name of each file should be output/displayed. even if that other solution works for removing file path and just leaving the file name and extension (for IE only), it doesn't work for showing the name of every file selected. it only shows the first file.
EDIT2:
Just in case you were wondering, i tried this too but it was a no go, but it seems like the answer posted here did work, so that's good.
var fileName = document.getElementById("myfiles").files;