API window.showDirectoryPicker 在所有基于 Chromium 的浏览器上都能正常工作。
但是,Firefox还没有这样的API。有什么解决办法吗?
奇怪的是,Firefox 有文件和目录句柄的 API,但没有选择器对话框。
您可以尝试使用
https://github.com/use-strict/file-system-access/tree/master中的
showDirectoryPicker
ponyfill。
index.html
文件中对我有用的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>HTML 5 Boilerplate</title>
</head>
<body>
<button onClick="showPicker()">Click mee</button>
<script type="module">
import { showDirectoryPicker } from 'https://cdn.jsdelivr.net/npm/file-system-access/lib/es2018.js';
const showPicker = async () => {
try {
const handle = await showDirectoryPicker();
console.log(handle);
} catch (e) {
console.log(e);
}
};
window.showPicker = showPicker;
</script>
</body>
</html>