【发布时间】:2017-12-04 03:57:48
【问题描述】:
我在玩drag-and-drop API,有两种方法可以从DragEvent.dataTransfer 收集文件,有readonly files: FileList 和readonly items: DataTransferItemList。
似乎items 是files 的超集,从items 收集File[] 更复杂,并且旧版IE 不支持items,因此files 更易于使用并且具有更好的支持,但是MDN上的文章首先使用items,只有在不支持时才切换到files。
我的问题是,如果我只需要从DragEvent 收集File[] 集合,我可以使用dataTransfer.files 属性还是dataTransfer.items 有一些好处,值得吗?
自我回答:
在现代浏览器 (chrome) 中,files 集合为空。被拖拽的文件只能通过items访问,不能只依赖files。
【问题讨论】:
-
和 dataTransfer.items 只有 Chrome 支持。根据caniuse.com,所以对chrome使用
.items属性,在其他地方使用.files -
但是你可以在stackoverflow answer 中查看 chrome 的空文件属性
-
看来
files集合不是空的,至少对于版本 63.0.3239.132(开发人员构建)(64 位)而言。