问题
I am trying to get the name of the files that are selected by a "file" input in HTML:
<input type="file" class="filestyle" name="file-select[]" id="file-select" accept="image/*" multiple>
I wrote this JavaScript code to do that:
$('#file-select').on("change", function(){
var selectedFiless = this.files;
for (var i = 0; i < selectedFiless.length; ++i) {
var name = selectedFiless.item(i).name;
alert(name);
}
});
However, I noticed that the names are sorted in alphabetical order, not in selection order. for example, if I select z.jpg and then a.jpg, when I print the names, I have a.jpg, z.jpg (alphabetical order). but I want z.jpg, a.jpg (selection order).
回答1:
It'll appear based on how you have sorted
in your system. I mean if you have set Sort by ascending in your system then irrespective or order you select, it will be selected in ascending order and vice versa for descending. I have tested in your code
and the results are as I have mentioned above.
A Demo to test in your local systems.
回答2:
FileList does not indicate any specific order of indexes corresponding to user selected files
来源:https://stackoverflow.com/questions/32857936/why-does-html-file-select-changes-the-order-of-selected-files-and-sort-them-al