是否可以与navigator.share共享文件(PDF)而不是url?

时间:2019-04-24 08:14:54

标签: javascript jquery progressive-web-apps

我有一个从服务器生成的pdf文件,我希望允许用户通过navigator.share共享此文件。是否可以共享文件而不是URL?

    navigator.share({
          title: 'Web Fundamentals',
          text: 'Check out Web Fundamentals — it rocks!',
          url: 'https://developers.google.com/web',
      })
        .then(() => console.log('Successful share'))
        .catch((error) => console.log('Error sharing', error));

3 个答案:

答案 0 :(得分:2)

Chrome 93.0.4570.0 现在支持通过 Web Share 共享 PDF 文件。请参阅 https://chromiumdash.appspot.com/commit/7d30d42a018d4aa76fca2896f2903d892b5f5284https://bugs.chromium.org/p/chromium/issues/detail?id=1006055

shareButton.onclick = async () => {
  const response = await fetch("https://example.com/files/hello.pdf");
  const buffer = await response.arrayBuffer();

  const pdf = new File([buffer], "hello.pdf", { type: "application/pdf" });
  const files = [pdf];

  // Share PDF file if supported.
  if (navigator.canShare({ files })) await navigator.share({ files });
};

答案 1 :(得分:1)

如果您要共享pdf文件,建议您将它们提供为指向private void loadImage(String URL){ // Your Glide code //Inside onLoadFailed call loadImage() again. //For number of attempts you can maintain one int and increment that on every attempt. } 属性的直接链接。请注意,“ url”指的是当前页面URL,就像在链接中一样。 也可以使用其他任何绝对或相对URL

答案 2 :(得分:0)

Chrome for Android(75+)现在支持此功能(called Web Share API Level 2),希望其他浏览器也能尽快效仿。

here上有一个演示。

相关问题