我已经下载了最新的chrome和firefox,我的代码执行以下操作:
所有这些类都是 FileSystem API 的一部分,但是,我在 Chrome 中收到此错误:
Uncaught (in promise) ReferenceError: FileSystemDirectoryEntry is not defined
如果我在错误之前记录该项目,则表明:
DirectoryEntry {
isFile: false,
isDirectory: true,
name: 'Videos',
fullPath: '/Videos',
filesystem: DOMFileSystem
}
执行日志记录的代码部分是:
/**
* Function is limited to traverse the system and limited to jpeg and png files
* @param item FileSystemEntry
* @param files - this input array will be mutated
* Note: The callbacks are promisified to allow for async/await.
*/
export async function readFileSystem(
item: FileSystemEntry,
files: File[],
types = /.(jpe?g|png)$/i
) {
console.log(item);
if (item instanceof FileSystemDirectoryEntry) {
const directoryReader = item.createReader();
...
所以我不知道这是否是一个错误或者我应该如何使用它。请问有什么解释吗?
我解决这个问题的方法是删除 check 实例,并使用这个函数:
export async function readFileSystem(
// same arguments than OP
) {
if (item.isDirectory) { // <---- only change here and interface below
const directoryReader = (item as FileSystemDirectoryEntry).createReader();
// ...
...
这是因为 Google Chrome 还不符合标准。您可以(重新)定义这样的名称
const FileSystemDirectoryEntry =
window.FileSystemDirectoryEntry || window.DirectoryEntry;
const FileSystemEntry = window.FileSystemEntry || window.Entry;
查看此了解详情。
.isFile
和 .isDirectory
标志来检查正在处理的内容:
export async function readFileSystem(item, files, type, types) {
if (item.isDirectory) {
const directoryReader = item.createReader();
// ...
} else if (item.isFile) {
// ...
} else {
throw new Error(`unknown type`);
}
}