What is Web Share API in JavaScript?

Share API (technically WebShare API) is a JavaScript-based API that enables web-based applications to share files across different domains. Introduced in Chrome version 37, it was created to simplify file-sharing between browsers, making it easier for users to collaborate on content.

The API is available as part of the FileSystem API and allows web applications to share files with other applications on the user’s device, and between devices. It is also possible to share files with remote servers, making it possible to collaborate on content in the cloud.

To share a file using the WebShare API, the first step is to get a reference to the FileSystem object. This is done by invoking the FileSystem.get() method, passing in the URL of the page that contains the file you want to share.

Once you have a reference to the FileSystem object, you can call the share() method, passing in the file you want to share, and the destination you want to share it with. The destination can be a file location on the user’s device, a remote server, or another application on the user’s device.

The following code shows how to share a file with a remote server.

var file = document.getElementById("file");
var fileSystem = window.FileSystem;
var remoteUrl = "https://someserver.com/file.txt";

fileSystem.get(file).then(function(fileSystem) {
    fileSystem
    	.share(file, remoteUrl)
    	.then(function(success) {
   			 console.log("File successfully shared!");
    	}, 
   	function(error) {
    console.log("Error sharing file: " + error);
    });
});
💡
Need a writer? Want to work with me? I have a subscription copywriting service available at Zipply.io. Subscribe to a set number of words and split it into as many copy assets as you need. Scale your startup, small business, or MVP. Upgrade, downgrade and pause at any time. Start your subscription here.