网络共享 API 不适用于保存在服务器上的图像

web share API is not working for images saved on server

我正在尝试为我的电子商务网站使用 navigator.share 选项。我可以发送标题、url 和文本,但无法附加图像。请参考以下代码

<html>


<body>
    <div>

        <div>
            <button class="share-btn" id="shareFilesButton">Share Files</button>
        </div>
    </div>
    
    <script>
     
    const shareBtn = document.querySelector('.share-btn');

    shareBtn.addEventListener('click', () => {
    
    // var file = new File([blob], "img1.jpeg", {type: 'image/jpeg'});
    // var filesArray = [file];
   if(navigator.canShare) {
    navigator.share({
      title: 'My awesome post!',
      text: 'This post may or may not contain the answer to the universe',
      url: window.location.href,
        files: ['img1.jpeg']
    }).then(() => {
      console.log('Thanks for sharing!');
    })
    .catch(err => {
      console.log(`Couldn't share because of`, err.message);
    });
  } else {
    console.log('web share not supported');
  }
});
</script>
</body>


</html>

在上面的示例中,我试图发送已在服务器上共享的图像。也很少有地方我得到 files: 作为关键,很少地方它的 file 发送图像。 我都试过了,但其中 none 有效。

我找到了答案。

if ('canShare' in navigator) {
  const share = async function(shareimg, shareurl, sharetitle, sharetext) {
    try {
      const response = await fetch(shareimg);
      const blob = await response.blob();
      const file = new File([blob], 'rick.jpg', {type: blob.type});

      await navigator.share({
        url: shareurl,
        title: sharetitle,
        text: sharetext,
        files: [file]
      });
    } catch (err) {
      console.log(err.name, err.message);
    }
  };
 
    document.querySelectorAll('.share-btn').forEach(item => {
  item.addEventListener('click', e => {
       str =   e.target.getAttribute('share');
    //   console.log(e.target.getAttribute('share'));
      str_array = str.split(',');
    //   console.log(str_array[1]);
    share(
      str_array[1],
      str_array[2],
      str_array[0],
      str_array[3]
    );
  })
})

 
}
 document.querySelectorAll('.share-btn').forEach(item => {
  item.addEventListener('click', e => {
       str =   e.target.getAttribute('share');
      console.log(e.target.getAttribute('share'));
      str_array = str.split(',');
      console.log(str_array[1]);
    share(
      'https://xtremetechie.com/share/img1.jpeg',
      'https://en.wikipedia.org/wiki/Rick_Astley',
      'Rick Astley',
      'Never gonna give you up!'
    );
  })
})
.share-btn {
  /*position: absolute;*/
  /*top: 50%;*/
  /*left: 50%;*/
  /*transform: translate(-50%, -50%);*/
  /*-ms-transform: translate(-50%, -50%);*/
  background-color: #555;
  color: white;
  font-size: 16px;
  /*padding: 12px 24px;*/
  border: none;
  cursor: pointer;
  border-radius: 5px;
  text-align: center;
}

.container .btn:hover {
  background-color: black;
}
.wrapper {
    width:250px;
    height:160px;
    float:left;
    border:1px solid black;
    margin:2px;
    text-align:text;
}
<html>


<body>
    <div>
        <div class="wrapper">
            <img src="https://xtremetechie.com/share/img2.jpg" width="200px"/><br>
            <button class="share-btn" id="shareFilesButton1" share="Test text1, https://xtremetechie.com/share/img2.jpg, https://xtremetechie.com/share/img2.jpg, Short Description for img1"  >Share </button>
        </div>
        <div class="wrapper">
            <img src="https://xtremetechie.com/share/img1.jpeg"  width="200px" /><br>
            <button class="share-btn" id="shareFilesButton" share="Test text2, https://xtremetechie.com/share/img1.jpeg, https://xtremetechie.com/share/img1.jpeg, Short Description for img2"  >Share</button>
        </div>

        <div>

           
        </div>
       


    </div>
</body>
</html>