FileSystemDirectoryReader.readEntries()使用时遇到的一个小坑

今天在处理网页上的拖拽上时遇到一个问题,说出来和大家分享一下。
需求: 在网页上拖拽上传一个多层文件夹


要上传文件夹,一个绕不开的问题就是如何读取到文件夹中的每一个文件或文件夹,这里我参考的是mdn上的方法(参考FileSystemDirectoryReader.readEntries()方法中的例子),这个例子在文件夹中的文件
数目小于100时,运行时没有问题的,可以读取到文件夹中的每个文件,但当文件数量大于100时,它最多只能读取到100个文件。


下面看一下mdn上对这个借口的介绍(没找到中文版的)


readEntries(successCallback[, errorCallback]);


successCallback
A function which is called when the directory's contents have been retrieved. The function receives a single input parameter: an array of file system entry objects, each based on FileSystemEntry. Generally, they are either FileSystemFileEntry objects, which represent standard files, or FileSystemDirectoryEntry objects, which represent directories. If there are no files left, or you've already called readEntries() on this FileSystemDirectoryReader, the array is empty.


errorCallback Optional
A callback function which is called if an error occurs while reading from the directory. It receives one input parameter: a FileError object describing the error which occurred.


请注意successCallback的介绍
如果需要读取目标对象是一个文件夹时,当文件夹对象FileSystemDirectoryReader中没有文件时,successCallback回调函数中的数组才会为空(该目录被完全读取),但实际上readEntries每次最多只能读取100个文件,这时候想读取剩余的文件,就
需要我们是用递归调用了


var fnReadEntries = function (entries) {
  entries.forEach(function (entry) {
    scanFiles(entry, directoryContainer);
  });
  if (entries.length > 0) {
    directoryReader.readEntries(fnReadEntries);
  }
};    
directoryReader.readEntries(fnReadEntries)


详细代码https://codepen.io/jichao/pen/XEpqjx
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值