我正在使用下面的代码,它使用 FileSystemAPI 和 webworker
const worker = new Worker( "worker.js" );
btn.onclick = async (evt) => {
const dirHandle = await showDirectoryPicker();
worker.postMessage( dirHandle );
};
worker.onmessage = (evt) => {
console.log( evt.data );
document.querySelector("pre").textContent = JSON.stringify( evt.data, (key, value) => {
if( value instanceof Blob ) {
return { name: value.name, size: value.size, type: value.type };
}
return value;
}, 4 );
}
worker.js
onmessage = async (evt) => {
const out = {};
const dirHandle = evt.data;
await handleDirectoryEntry( dirHandle, out );
postMessage( 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 );
}
}
}
https://glitch.com/edit/#!/navigate-sub-directory-from-a-worker?path=worker.js%3A5%3A21
以上为小型数据集工作,但目录包含超过 1000 个文件,但它不起作用。
任何帮助或输入表示赞赏。谢谢!