使用 showDirectoryPicker() 访问子目录文件内容
Accessing sub-directory file content using showDirectoryPicker()
如何使用 File System Access API 访问所选目录的文件夹中包含的文件?
document.querySelector('button').addEventListener('click', async () => {
const dirHandle = await window.showDirectoryPicker();
for await (const entry of dirHandle.values()) {
if (entry.kind === "file"){
const file = await entry.getFile();
const text = await file.text();
console.log(text);
}
if (entry.kind === "directory"){
/* for file in this directory do something */
}
}
});
<button>Choose Directory</button>
您需要调用 dirHandle.getDirectoryHandle(name, options)
方法,并将 name
参数设置为条目的 .name
。
这是一个示例代码,它将遍历传递的目录并构建它找到的文件的树。
btn.onclick = async (evt) => {
const out = {};
const dirHandle = await showDirectoryPicker();
await handleDirectoryEntry( dirHandle, out );
console.log( out );
};
async function handleDirectoryEntry( dirHandle, out ) {
for await (const entry of dirHandle.values()) {
if (entry.kind === "file"){
const file = await entry.getFile();
out[ file.name ] = file;
}
if (entry.kind === "directory") {
const newHandle = await dirHandle.getDirectoryHandle( entry.name, { create: false } );
const newOut = out[ entry.name ] = {};
await handleDirectoryEntry( newHandle, newOut );
}
}
}
凯多回答的小改进:
btn.onclick = async (evt) => {
const out = {};
const dirHandle = await showDirectoryPicker();
await handleDirectoryEntry( dirHandle, out );
console.log( out );
};
async function handleDirectoryEntry( dirHandle, out ) {
for await (const entry of dirHandle.values()) {
if (entry.kind === "file"){
const file = await entry.getFile();
out[ file.name ] = file;
}
if (entry.kind === "directory") {
const newOut = out[ entry.name ] = {};
await handleDirectoryEntry( entry, newOut );
}
}
}
dirHandle.values()
returns 继承自 FileSystemHandle
的对象列表,有两种可能性:FileSystemFileHandle
或 FileSystemDirectoryHandle
.
因为 const entry
已经是 FileSystemDirectoryHandle
如果 entry.kind
是 "directory"
则不需要调用 dirHandle.getDirectoryHandle()
如何使用 File System Access API 访问所选目录的文件夹中包含的文件?
document.querySelector('button').addEventListener('click', async () => {
const dirHandle = await window.showDirectoryPicker();
for await (const entry of dirHandle.values()) {
if (entry.kind === "file"){
const file = await entry.getFile();
const text = await file.text();
console.log(text);
}
if (entry.kind === "directory"){
/* for file in this directory do something */
}
}
});
<button>Choose Directory</button>
您需要调用 dirHandle.getDirectoryHandle(name, options)
方法,并将 name
参数设置为条目的 .name
。
这是一个示例代码,它将遍历传递的目录并构建它找到的文件的树。
btn.onclick = async (evt) => {
const out = {};
const dirHandle = await showDirectoryPicker();
await handleDirectoryEntry( dirHandle, out );
console.log( out );
};
async function handleDirectoryEntry( dirHandle, out ) {
for await (const entry of dirHandle.values()) {
if (entry.kind === "file"){
const file = await entry.getFile();
out[ file.name ] = file;
}
if (entry.kind === "directory") {
const newHandle = await dirHandle.getDirectoryHandle( entry.name, { create: false } );
const newOut = out[ entry.name ] = {};
await handleDirectoryEntry( newHandle, newOut );
}
}
}
凯多回答的小改进:
btn.onclick = async (evt) => {
const out = {};
const dirHandle = await showDirectoryPicker();
await handleDirectoryEntry( dirHandle, out );
console.log( out );
};
async function handleDirectoryEntry( dirHandle, out ) {
for await (const entry of dirHandle.values()) {
if (entry.kind === "file"){
const file = await entry.getFile();
out[ file.name ] = file;
}
if (entry.kind === "directory") {
const newOut = out[ entry.name ] = {};
await handleDirectoryEntry( entry, newOut );
}
}
}
dirHandle.values()
returns 继承自 FileSystemHandle
的对象列表,有两种可能性:FileSystemFileHandle
或 FileSystemDirectoryHandle
.
因为 const entry
已经是 FileSystemDirectoryHandle
如果 entry.kind
是 "directory"
则不需要调用 dirHandle.getDirectoryHandle()