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

javascript - How to share a single Base64 URL Image via the Web share API? - Stack Overflow

programmeradmin1浏览0评论

So, I need to share a base64 Image string using the Web Share API on TypeScript.

Would it be ok if I passed the base64 string as the url parameter like this:

  var base64url = "data:image/octet-stream;base64,/9j/4AAQSkZ...."
  navigator.share({
    title: 'Hello',
    text: 'Check out this image!',
    url: base64url ,
  })

Or do I need to have a Files Array and use Web Share API v2 ? If so, how do I implement it, all I have is a base64 string and the examples I could found were too confusing.

I am a noob in this matter so some help would be greatly appreciated!

So, I need to share a base64 Image string using the Web Share API on TypeScript.

Would it be ok if I passed the base64 string as the url parameter like this:

  var base64url = "data:image/octet-stream;base64,/9j/4AAQSkZ...."
  navigator.share({
    title: 'Hello',
    text: 'Check out this image!',
    url: base64url ,
  })

Or do I need to have a Files Array and use Web Share API v2 ? If so, how do I implement it, all I have is a base64 string and the examples I could found were too confusing.

I am a noob in this matter so some help would be greatly appreciated!

Share Improve this question asked Apr 16, 2020 at 12:16 BenBen 811 silver badge2 bronze badges 2
  • It will work but most platforms where you will share this URL will not show it like a link it will just appear as a text, and most non tech people don't even know what a base64 url is , chances are very low that they will copy the URL and paste in the browser. – Yanjan. Kaf. Commented Apr 16, 2020 at 12:24
  • 1 @blacksheep Ok, so how do I share a proper image file via the web share API, could you help? – Ben Commented Apr 16, 2020 at 12:31
Add a ment  | 

1 Answer 1

Reset to default 9

As mentioned in the ments, sharing the url in the url field won't end up in a nice result for your users.

You can use Web Share API v2 with the following code:

const base64url = "data:image/octet-stream;base64,/9j/4AAQSkZ...."
const blob = await (await fetch(base64url)).blob();
const file = new File([blob], 'fileName.png', { type: blob.type });
navigator.share({
  title: 'Hello',
  text: 'Check out this image!',
  files: [file],
})

You might have to tweak the extension and mime type depending on your actual image urls.

Be aware though that v2 is currently only available on Android Chrome (I can't find a good source for that but I just did this on a project). You can check for availability at run time by using navigator.canShare().

发布评论

评论列表(0)

  1. 暂无评论