Skip to content

Instantly share code, notes, and snippets.

@happymishra
Last active June 29, 2019 09:17
Show Gist options
  • Save happymishra/626138ede11c3a2b0238034e39d1f978 to your computer and use it in GitHub Desktop.
Save happymishra/626138ede11c3a2b0238034e39d1f978 to your computer and use it in GitHub Desktop.
var shareButton = document.getElementById('share-button');
shareButton.addEventListener('click', function () {
var filesArray = document.getElementById('share-files').files
var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Adding title afterwards as navigator.canShare just
// takes files as input
shareData.title = "Bits and pieces"
navigator.share(shareData)
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
} else {
console.log("Your system doesn't support sharing files.");
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment