Google Chrome 错误“FileSystemDirectoryEntry”不存在

问题描述 投票:0回答:3

我已经下载了最新的chrome和firefox,我的代码执行以下操作:

  1. 接收到FileSystemEntry并且
  2. FileSystemDirectoryEntry
  3. 做一件事
  4. 另一个FileSystemFileEntry

所有这些类都是 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();
    ...

所以我不知道这是否是一个错误或者我应该如何使用它。请问有什么解释吗?

javascript google-chrome firefox
3个回答
0
投票

我解决这个问题的方法是删除 check 实例,并使用这个函数:

export async function readFileSystem(
// same arguments than OP
) {
  if (item.isDirectory) { // <---- only change here and interface below
    const directoryReader = (item as FileSystemDirectoryEntry).createReader();
   // ...
...

0
投票

这是因为 Google Chrome 还不符合标准。您可以(重新)定义这样的名称

const FileSystemDirectoryEntry =
  window.FileSystemDirectoryEntry || window.DirectoryEntry;
const FileSystemEntry = window.FileSystemEntry || window.Entry;

查看了解详情。


0
投票

不要检查阶级关系,而是使用

.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`);
  }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.