对FileList对象进行排序

Dar*_*ava 8 javascript html5 file

我正在尝试使用新的File API对输入文件进行排序.它返回的列表似乎是不可变的:

var x = "";
var files = e.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.

files.sort();

> Uncaught TypeError: Object #<FileList> has no method 'sort' 
Run Code Online (Sandbox Code Playgroud)

如果我想一次读入多个文件,但我希望它们按顺序到达.(A.csv之前处理B.csv等).这可以实现吗?

Jan*_*oom 12

[].slice.call(files)使它成为一个真正的数组,然后你可以使用.sort它.

  • 由于这个原因,ES6中的一个新方法是[Array.from](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from).截至撰写之日,它仅由Firefox 32及更高版本实现. (2认同)
  • 使用ES6扩展运算符是另一种选择:let files = [... e.target.files] .sort(); (2认同)