why does “HTML file select” changes the order of selected files and sort them alphabetically?

三世轮回 提交于 2021-02-08 05:13:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!