I would like to share a JSON object as a file via the Web Share API.
However, when specifying the type
as application/json
, I got the DOMException: Permission denied
Error:
navigator.share({
files: [new File(["{}"], "test.json", {type: "application/json"})]
})
// Uncaught (in promise) DOMException: Permission denied
However, if I change the type
to text/plain
and the file name extension to .txt
, it works as expected:
navigator.share({
files: [new File(["{}"],"test.txt", {type: "text/plain"})]
})
// File share success
I would like to have it as a `JSON file to be shared instead.
Browser: Microsoft Edge (Chromium) 96.0.1054.43
Any helps would be appreciated.
Snippet Example:
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>
This is working as intended. You can see the list of supported file types in this document.