Web Share API 共享文件权限被拒绝

时间:2021-06-04 21:52:29

标签: web-share

我不确定我在这里做错了什么,我认为应该有更多的文档或更好的错误描述来说明这个 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。我真的不明白我应该寻找什么。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我看到您已经收到了一些关于您提出的 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