【问题标题】:It is possible to share a file(PDF) instead of url with navigator.share?是否可以与 navigator.share 共享文件(PDF)而不是 url?
【发布时间】:2019-04-24 08:14:54
【问题描述】:

我有一个从服务器生成的 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));

【问题讨论】:

    标签: javascript jquery progressive-web-apps


    【解决方案1】:

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

    here 提供演示。

    【讨论】:

    【解决方案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 });
    };
    
    

    【讨论】:

      【解决方案3】:

      如果您想分享您的 pdf 文件,我建议您将它们作为指向 url 属性的直接链接提供。请注意,'' 的 url 指的是当前页面 URL,就像它在链接中一样。 也可以使用任何其他绝对或相对 URL

      【讨论】:

      • 目前Web Share api不支持文件共享,所以我会尽量听从你的建议。谢谢。
      • 欢迎您,希望对您有所帮助。如果成功实施,请接受我的回答。谢谢:)
      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2017-10-06
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多