Lag*_*hen 11 javascript reactjs
我正在尝试阅读和写作,File System Web API
但我不断发现TypeError
Uncaught (in promise) TypeError: window.showOpenFilePicker is not a function
,我不知道发生了什么。
这是反应代码片段:
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;
Run Code Online (Sandbox Code Playgroud)
所以我认为它在 React 中不起作用,然后我尝试了 Vanilla Javascript,但我仍然不断进入TypeError
Uncaught (in promise) TypeError: window.showOpenFilePicker is not a function
控制台。这是代码片段。
<!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>
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
小智 9
我想您遇到上述问题的原因如下:
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
考虑查看文档: https: //developer.mozilla.org/en-US/docs/Web/API/Window/showOpenFilePicker
归档时间: |
|
查看次数: |
13666 次 |
最近记录: |