WebShare
Namespace: Wisej.Ext.WebShare
Assembly: Wisej.Ext.WebShare (0.0.0.0)
Provides methods for sharing text, links, files, and other content to an arbitrary share target selected by the user.
- C#
- VB.NET
public static class WebShare
Public Class WebShare
Can only be used from https or localhost. See: Web Share API.
Example:
The following example checks whether the browser supports sharing and, if so, shares a link:
private async void buttonShare_Click(object sender, EventArgs e)
{
if (await WebShare.CanShare())
{
await WebShare.ShareAsync(
url: "https://wisej.com",
text: "Check out Wisej.NET!",
title: "Wisej.NET");
}
else
{
AlertBox.Show("Sharing is not supported by this browser.");
}
}
Methods
CanShare()
Returns whether the browser is capable of performing a share operation.
Returns: Task<Object>. A task that resolves to true when the browser supports the Web Share API; otherwise false.
This method only checks whether the navigator.share function exists in the browser. To verify that a specific set of data can be shared, use CanShareAsync.
Example:
The following example hides a share button when the browser does not support sharing:
protected override async void OnLoad(EventArgs e)
{
base.OnLoad(e);
bool supported = await WebShare.CanShare();
this.buttonShare.Visible = supported;
}
CanShareAsync(url, text, title, fileStreams)
The CanShareAsync method of the Web Share API returns true if the equivalent call to ShareAsync would succeed.
| Parameter | Type | Description |
|---|---|---|
| url | String | A string representing a URL to be shared. |
| text | String | A string representing text to be shared. |
| title | String | A string representing the title to be shared. |
| fileStreams | FileStream[] | An array of files representing files to be shared. |
Returns: Task<Object>. The result from the client: true if the data can be shared; otherwise false.
Use this method to validate the data before calling ShareAsync, for example to check whether the browser allows sharing the given file types. The method returns false if the data cannot be shared. See: Navigator.canShare().
Example:
The following example checks whether a PDF file can be shared before sharing it:
private async void buttonShareReport_Click(object sender, EventArgs e)
{
var path = Application.MapPath("Reports/report.pdf");
using (var check = File.OpenRead(path))
{
if (!await WebShare.CanShareAsync(fileStreams: new[] { check }))
{
AlertBox.Show("This file cannot be shared from this browser.");
return;
}
}
using (var file = File.OpenRead(path))
{
await WebShare.ShareAsync(title: "Monthly Report", fileStreams: new[] { file });
}
}
ShareAsync(url, text, title, fileStreams)
The ShareAsync method of the Web Share API invokes the native sharing mechanism of the device to share data such as text, URLs, or files.
| Parameter | Type | Description |
|---|---|---|
| url | String | A string representing a URL to be shared. |
| text | String | A string representing text to be shared. |
| title | String | A string representing the title to be shared. |
| fileStreams | FileStream[] | An array of files representing files to be shared. |
Returns: Task<Object>. The result from the client.
The browser requires the share operation to be triggered by a user action, such as a button click. The returned task fails if the user cancels the share dialog or if the data cannot be shared. See: Navigator.share().
Example:
The following example shares a link and an image from a button click:
private async void buttonShare_Click(object sender, EventArgs e)
{
try
{
using (var image = File.OpenRead(Application.MapPath("Images/photo.png")))
{
await WebShare.ShareAsync(
url: "https://wisej.com",
text: "Look at this photo!",
title: "My Photo",
fileStreams: new[] { image });
}
}
catch (Exception ex)
{
// The user canceled the share dialog or the data could not be shared.
AlertBox.Show(ex.Message);
}
}