Attachments: Loading Attachments

There are a few methods that allow you to download attachments from a database:

session.Advanced.Attachments.Get can be used to download an attachment.
session.Advanced.Attachments.GetNames can be used to download all attachment names that are attached to a document.
session.Advanced.Attachments.GetRevision can be used to download an attachment of a revision document.
session.Advanced.Attachments.Exists can be used to determine if an attachment exists on a document.

Syntax

AttachmentResult Get(string documentId, string name);
AttachmentResult Get(object entity, string name);
AttachmentName[] GetNames(object entity);
AttachmentResult GetRevision(string documentId, string name, string changeVector);
bool Exists(string documentId, string name);
Task<AttachmentResult> GetAsync(string documentId, string name, CancellationToken token = default);
Task<AttachmentResult> GetAsync(object entity, string name, CancellationToken token = default);
Task<AttachmentResult> GetRevisionAsync(string documentId, string name, string changeVector, CancellationToken token = default);
Task<bool> ExistsAsync(string documentId, string name, CancellationToken token = default);

Example

using (var session = store.OpenSession())
{
    Album album = session.Load<Album>("albums/1");

    using (AttachmentResult file1 = session.Advanced.Attachments.Get(album, "001.jpg"))
    using (AttachmentResult file2 = session.Advanced.Attachments.Get("albums/1", "002.jpg"))
    {
        Stream stream = file1.Stream;

        AttachmentDetails attachmentDetails = file1.Details;
        string name = attachmentDetails.Name;
        string contentType = attachmentDetails.ContentType;
        string hash = attachmentDetails.Hash;
        long size = attachmentDetails.Size;
        string documentId = attachmentDetails.DocumentId;
        string changeVector = attachmentDetails.ChangeVector;
    }

    AttachmentName[] attachmentNames = session.Advanced.Attachments.GetNames(album);
    foreach (AttachmentName attachmentName in attachmentNames)
    {
        string name = attachmentName.Name;
        string contentType = attachmentName.ContentType;
        string hash = attachmentName.Hash;
        long size = attachmentName.Size;
    }

    bool exists = session.Advanced.Attachments.Exists("albums/1", "003.jpg");
}
using (var asyncSession = store.OpenAsyncSession())
{
    Album album = await asyncSession.LoadAsync<Album>("albums/1");

    using (AttachmentResult file1 = await asyncSession.Advanced.Attachments.GetAsync(album, "001.jpg"))
    using (AttachmentResult file2 = await asyncSession.Advanced.Attachments.GetAsync("albums/1", "002.jpg"))
    {
        Stream stream = file1.Stream;

        AttachmentDetails attachmentDetails = file1.Details;
        string name = attachmentDetails.Name;
        string contentType = attachmentDetails.ContentType;
        string hash = attachmentDetails.Hash;
        long size = attachmentDetails.Size;
        string documentId = attachmentDetails.DocumentId;
        string changeVector = attachmentDetails.ChangeVector;
    }

    AttachmentName[] attachmentNames = asyncSession.Advanced.Attachments.GetNames(album);
    foreach (AttachmentName attachmentName in attachmentNames)
    {
        string name = attachmentName.Name;
        string contentType = attachmentName.ContentType;
        string hash = attachmentName.Hash;
        long size = attachmentName.Size;
    }

    bool exists = await asyncSession.Advanced.Attachments.ExistsAsync("albums/1", "003.jpg");
}