【问题标题】:Web Share API sharing files Permission DeniedWeb Share API 共享文件权限被拒绝
【发布时间】:2021-06-04 21:52:29
【问题描述】:

我不确定我在这里做错了什么,我认为这个Web Share API 应该有更多的文档或更好的错误描述。

我正在尝试共享以下文件

{
  lastModified: 1622843015507
  lastModifiedDate: Fri Jun 04 2021 16:43:35 GMT-0500 (Eastern Standard Time) {}
  name: "60b1d17b7f2cd71c8307fae2"
  size: 37835
  type: "image/png"
  webkitRelativePath: ""
}

使用

await navigator.share({
    text: 'TEST',
    files: [file],
  });

我已确定该类型是允许的类型,但我不断收到DOMException: Permission denied。我真的不明白我应该寻找什么。

【问题讨论】:

    标签: web-share


    【解决方案1】:

    我认为您面临的问题是您的文件名没有扩展名。尝试在文件名中添加.png,它应该会自动工作。

    const file = new File(['hello'], 'hello.png', { type: 'image/png' });
    await navigator.share({ files: [file] });
    

    【讨论】:

      【解决方案2】:

      我看到你已经收到了一些关于你已经提出的issue 的反馈。下面是一个具体示例,希望能帮助您入门:

      if (navigator.canShare && navigator.canShare({ files: filesArray })) {
        navigator.share({
          files: filesArray,
          title: 'Vacation Pictures',
          text: 'Photos from September 27 to October 14.',
        })
        .then(() => console.log('Share was successful.'))
        .catch((error) => console.log('Sharing failed', error));
      } else {
        console.log(`Your system doesn't support sharing files.`);
      }
      

      获取文件数组(上面的filesArray)的一种方法是通过<input type=files> 元素。请参阅official example

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-05
        • 2021-06-23
        • 2012-05-31
        • 2021-02-28
        • 1970-01-01
        • 2023-03-19
        • 2022-06-23
        • 2013-11-27
        相关资源
        最近更新 更多