1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>FileSystem API PRC.</title> </head> <body> <button id="chooseDir">选择文件夹</button> </body> <script> const btn = document.querySelector("#chooseDir") btn.onclick = async function (){ let handle = await showDirectoryPicker() const root = await processHandle(handle) console.log(root)
const fileHandle = root.children[1] const file = await fileHandle.getFile() const reader = new FileReader() reader.onload = function (e){ console.log(e.target.result) } reader.readAsDataURL(file) } async function processHandle(handle){ if( handle.kind === 'file' ){ return handle; } handle.children = [] let iter = handle.entries() for await (const item of iter){ handle.children.push(await processHandle(item[1])) } return handle; }
</script> </html>
|