【发布时间】:2021-12-06 10:10:31
【问题描述】:
我想通过 Web 共享 API 将 JSON 对象作为文件共享。
但是,当将type 指定为application/json 时,我得到了DOMException: Permission denied 错误:
navigator.share({
files: [new File(["{}"], "test.json", {type: "application/json"})]
})
// Uncaught (in promise) DOMException: Permission denied
但是,如果我将 type 更改为 text/plain 并将文件扩展名更改为 .txt,它会按预期工作:
navigator.share({
files: [new File(["{}"],"test.txt", {type: "text/plain"})]
})
// File share success
我想把它作为一个 `JSON 文件来共享。
浏览器:Microsoft Edge (Chromium) 96.0.1054.43
任何帮助将不胜感激。
片段示例:
const textbtn = () => {
navigator.share({
files: [new File(["{}"],"test.txt", {type: "text/plain"})]
}).catch(e => alert(e.message))
}
const jsonbtn = () => {
navigator.share({
files: [new File(["{}"],"test.json", {type: "application/json"})]
}).catch(e => alert(e.message))
}
<h1>WebShare Test<h1>
<button onclick="jsonbtn()">test.json | application/json</button>
<br />
<button onclick="textbtn()">text.text | text/pain</button>
【问题讨论】:
标签: javascript node.js json permissions web-share