【问题标题】:Web Share API: Permission Denied on certain file typeWeb 共享 API:某些文件类型的权限被拒绝
【发布时间】: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


    【解决方案1】:

    根据MDN documentation about shareable file types,支持的文件类型只有很少。

    • PDF
    • 一些音频文件
    • 一些图片文件
    • 一些文本文件
    • 一些视频文件

    特别是文本文件是:

    .css - text/css
    .csv - text/csv
    .ehtml - text/html
    .htm - text/html
    .html - text/html
    .shtm - text/html
    .shtml - text/html
    .text - text/plain
    .txt - text/plain
    

    很遗憾,JSON(application/json)不在其中,但 txt(text/plain)在其中。

    根据您的情况,您可能需要考虑通过 URL parameter 共享相关文件的 URL。

    【讨论】:

      【解决方案2】:

      这是按预期工作的。您可以在此文档中看到list of supported file types

      【讨论】:

      • 感谢您的信息!这是 Chromium 特有的东西,还是标准?还有可以参考的官方文档(例如MDN、Chromium源代码)吗?
      • 该文档的标题是“Web 共享 - Chromium 中允许的文件扩展名”,就是这样。该标准没有假设浏览器供应商可以共享哪些内容。我刚刚打开了一个PR,将这个信息添加到 MDN (preview)。
      • @DenverCoder9 这似乎是 MDN 上的官方列表:developer.mozilla.org/en-US/docs/Web/API/Navigator/…
      • 没错。是我的PR 得到了这个……:-)
      • 你太棒了&lt;3,但不用担心,你回答了这个问题,你得到了荣誉。刚刚对你的答案投了赞成票!
      猜你喜欢
      • 1970-01-01
      • 2020-03-05
      • 2021-06-23
      • 2012-05-31
      • 2021-02-28
      • 1970-01-01
      • 2023-03-19
      • 2022-06-23
      • 1970-01-01
      相关资源
      最近更新 更多