Wener Site

FileSystem

约 1 分钟阅读
JavaScript
const dirHandle = await window.showDirectoryPicker();

const saveFileHandle = await self.showSaveFilePicker({
  suggestedName: 'Untitled Text.txt',
  types: [
    {
      description: 'Text documents',
      accept: {
        'text/plain': ['.txt'],
      },
    },
  ],
});

const openFileHandle = await self.showOpenFilePicker({
  // desktop, documents, downloads, music, pictures, videos
  startIn: 'pictures',
  // startIn: dirHandle,
});

// 创建目录
const neoDirHandle = await dirHandle.getDirectoryHandle('neo', { create: true });
// 创建文件
const readmeFileHandle = await neoDirHandle.getFileHandle('README.txt', { create: true });
// 删除文件
await neoDirHandle.removeEntry('README.txt');
// 递归删除目录
await dirHandle.removeEntry('neo', { recursive: true });

Origin Private File System

  • OPFS - Origin Private File System
  • chromestatus
    • dev 94, trail 95, shiping 99
JavaScript
// FileSystemDirectoryHandle
const root = await navigator.storage.getDirectory();

Drop as Handle

drop-handle.js
elem.addEventListener('dragover', (e) => {
  // Prevent navigation.
  e.preventDefault();
});

elem.addEventListener('drop', async (e) => {
  // Prevent navigation.
  e.preventDefault();
  // Process all of the items.
  for (const item of e.dataTransfer.items) {
    // Careful: `kind` will be 'file' for both file
    // _and_ directory entries.
    if (item.kind === 'file') {
      const entry = await item.getAsFileSystemHandle();
      if (entry.kind === 'directory') {
        handleDirectoryEntry(entry);
      } else {
        handleFileEntry(entry);
      }
    }
  }
});

关联信息

反向链接和本文引用的外部资料。

References

GitHub

2 条

其他外链

8 条
最近更新commit f4a6b2dEdit

On this page