gpt4 book ai didi

javascript - 未捕获( promise 中)TypeError : window. showOpenFilePicker 不是函数

转载 作者:行者123 更新时间:2023-12-05 00:38:13 26 4
gpt4 key购买 nike

Uncaught (in promise) TypeError: window.showOpenFilePicker() is not a function
我正在尝试阅读和写作 File System Web API但我不断收到TypeError Uncaught (in promise) TypeError: window.showOpenFilePicker is not a function ,我不知道发生了什么。
这是 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 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>
知道我做错了什么吗?

最佳答案

我想你看到上面的问题是因为以下原因: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

关于javascript - 未捕获( promise 中)TypeError : window. showOpenFilePicker 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69591311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com