最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - It is possible to share a file(PDF) instead of url with navigator.share? - Stack Overflow

programmeradmin3浏览0评论

I have a pdf file that is generated from server and I would like to allow the user to share this file via navigator.share. Is it possible to share a file instead of URL?

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

I have a pdf file that is generated from server and I would like to allow the user to share this file via navigator.share. Is it possible to share a file instead of URL?

    navigator.share({
          title: 'Web Fundamentals',
          text: 'Check out Web Fundamentals — it rocks!',
          url: 'https://developers.google./web',
      })
        .then(() => console.log('Successful share'))
        .catch((error) => console.log('Error sharing', error));
Share Improve this question edited Apr 24, 2019 at 8:17 ilkerkaran 4,3443 gold badges29 silver badges44 bronze badges asked Apr 24, 2019 at 8:14 maga42maga42 631 silver badge7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Chrome 93.0.4570.0 now supports sharing PDF files with Web Share. See https://chromiumdash.appspot./mit/7d30d42a018d4aa76fca2896f2903d892b5f5284 and https://bugs.chromium/p/chromium/issues/detail?id=1006055

shareButton.onclick = async () => {
  const response = await fetch("https://example./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 });
};

Chrome for Android (75+) now supports this feature (called Web Share API Level 2) and hopefully other browsers will follow suit soon.

A demo is available here.

If you want to share your pdf file, I will suggest that you provide them as direct links to url property. Note that a url of '' refers to the current page URL, just as it would in a link. Any other absolute or relative URL can also be used.

发布评论

评论列表(0)

  1. 暂无评论