javascript - How do I grab the names of multiple files that are about to be uploaded? -
javascript - How do I grab the names of multiple files that are about to be uploaded? -
i have next form , div , jquery. when selecting file uploaded, name of file (with it's extension) output div, however, doesn't work multiple files. when selecting multiple files, name of first file output. looked on solution multiple files, , 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 list of file names (with extensions) should happen files selected, not after form beingness submitted.
edit:
on top of said below, thing happens when single file selected, name of file shows next browse button, when multiple files selected, doesn't show name of each file, says "# files selected." firefox , "# files" chrome. went see ie , 1 different. puts path of file or files in text field next browse button, guess that's other question , reply about. tried of these , none work, , if 1 of them did, seems net explorer thing. need cross browser solution.
var filename = $(this).val().split('\\').pop(); var filename = $(this).val().split(',').pop(); var filename = $(this).val().split(', ').pop();
so recap, when selecting multiple files, name of each file should output/displayed. if other solution works removing file path , leaving file name , extension (for ie only), doesn't work showing name of every file selected. shows first file.
edit2:
just in case wondering, tried no go, seems reply posted here did work, that's good.
var filename = document.getelementbyid("myfiles").files;
you can utilize files
property of file input
element list of files selected. property contains filelist
object can utilize this:
$("input:file").change(function () { var filenames = ''; (var = 0; < this.files.length; i++) { filenames += '<li>' + this.files[i].name + '</li>'; } $(".filename").html('<ul>' + filenames + '</ul>'); });
see demo.
if want list of files separated spaces or commas:
$("input:file").change(function () { var filenames = $.map(this.files, function (file) { homecoming file.name; }); $(".filename").html(filenames.join(', ')); });
see demo.
javascript jquery forms
Comments
Post a Comment