Add Attachment Example

Example to add a zip file as an attachment

public void ShareAttachment(string title, string path, string mimeType, string filename)
{
    ShareSheet shareSheet = ShareSheet.CreateInstance();
    shareSheet.AddText(title);
    StartCoroutine(GetFileAsByteArray(path, (data) =>
    {
        shareSheet.AddAttachment(data, mimeType, filename);
        shareSheet.Show();
        
    }));
}

#region Helpers
private IEnumerator GetFileAsByteArray(string filePath, Action<byte[]> callback)
{
    var sanitizedFilePath = GetSanitizedFilePath(filePath);
    UnityWebRequest request = UnityWebRequest.Get(sanitizedFilePath);
    yield return request.SendWebRequest();

    Debug.LogError("File path : " + sanitizedFilePath);
    if (request.isNetworkError || request.isHttpError)
    {
        Debug.LogError(request.error);
        callback?.Invoke(null);
    }
    else
    {
        callback?.Invoke(request.downloadHandler.data);
    }
}

private string GetSanitizedFilePath(string filePath)
{
    if (filePath == null)
        return null;

    if (filePath.StartsWith("jar:") || filePath.StartsWith("file:"))
        return filePath;

    return Path.Combine("file://", filePath);
}
#endregion

Usage

ShareAttachment("Sharing a zip file", Path.Combine(Application.streamingAssetsPath, "test.zip"), "application/zip", "test");

Last updated

Was this helpful?