今天在处理网页上的拖拽上时遇到一个问题,说出来和大家分享一下。
需求: 在网页上拖拽上传一个多层文件夹
要上传文件夹,一个绕不开的问题就是如何读取到文件夹中的每一个文件或文件夹,这里我参考的是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
需求: 在网页上拖拽上传一个多层文件夹
要上传文件夹,一个绕不开的问题就是如何读取到文件夹中的每一个文件或文件夹,这里我参考的是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