未捕获的 Promise 错误:TypeError: window.showOpenFilePicker() 不是一个函数
我正在尝试使用文件系统 Web API 进行读写操作,但是我一直收到 TypeError 错误:未捕获的 Promise 错误:TypeError: window.showOpenFilePicker 不是一个函数
,我不知道出了什么问题。
以下是 React 代码片段:
const FileReader = () => {
const openThisFile = async () => {
const [fileHandle] = await window.showOpenFilePicker(); // error: Property 'showOpenFilePicker' does not exist on type 'Window & typeof globalThis'
console.log(fileHandle);
};
return (
<div>
<button onClick={openThisFile}>Open file</button>
</div>
);
};
export default FileReader;
所以我认为它在React中无法工作,然后我尝试了Vanilla JavaScript,但仍然在控制台中不断出现TypeError
Uncaught ( in promise ) TypeError:window.showOpenFilePicker不是一个函数
。以下是代码片段。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Open file</title>
</head>
<body>
<button onclick="getFile()">Open file</button>
<script>
const pickerOpts = {
types: [
{
description: "Images",
accept: {
"image/*": [".png", ".gif", ".jpeg", ".jpg"],
},
},
],
excludeAcceptAllOption: true,
multiple: false,
};
let fileHandle;
async function getFile() {
[fileHandle] = await window.showOpenFilePicker(pickerOpts);
// run code with our fileHandle
}
console.log(fileHandle);
</script>
</body>
</html>
你知道我做错了什么吗?