Attachments: Loading Attachments


Learn in this page how to load a part of an attachment, an entire attachment, or multiple attachments.


Load attachments

  • Use these methods to load attachments from the database.

    • session.Advanced.Attachments.Get
      Can be used to download an attachment or multiple attachments.
    • 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.
  • Use this method to verify that an attachment exists.

    • session.Advanced.Attachments.Exists

Syntax

AttachmentResult Get(string documentId, string name);
AttachmentResult Get(object entity, string name);
IEnumerator<AttachmentEnumeratorResult> Get(IEnumerable<AttachmentRequest> attachments);
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<IEnumerator<AttachmentEnumeratorResult>> GetAsync(IEnumerable<AttachmentRequest> attachments, 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 I

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");
}

Example II

Here, we load multiple string attachments we previously created for a document. We then go through them, and decode each attachment to its original text.

// Load a user profile
var user = session.Load<User>(userId);

// Get the names of files attached to this document
IEnumerable<AttachmentRequest> attachmentNames = session.Advanced.Attachments.GetNames(user).Select(x => new AttachmentRequest(userId, x.Name));

// Get the attached files
IEnumerator<AttachmentEnumeratorResult> attachmentsEnumerator = session.Advanced.Attachments.Get(attachmentNames);

// Go through the document's attachments
while (attachmentsEnumerator.MoveNext())
{
    AttachmentEnumeratorResult res = attachmentsEnumerator.Current;

    AttachmentDetails attachmentDetails = res.Details; // attachment details

    Stream attachmentStream = res.Stream; // attachment contents

    // In this case it is a string attachment, that can be decoded back to text 
    var ms = new MemoryStream();
    attachmentStream.CopyTo(ms);
    string decodedStream = Encoding.UTF8.GetString(ms.ToArray());
}
// Load a user profile
var user = await session.LoadAsync<User>(userId);

// Get the names of files attached to this document
IEnumerable<AttachmentRequest> attachmentNames = session.Advanced.Attachments.GetNames(user).Select(x => new AttachmentRequest(userId, x.Name));

// Get the attached files
IEnumerator<AttachmentEnumeratorResult> attachmentsEnumerator = await session.Advanced.Attachments.GetAsync(attachmentNames);

// Go through the document's attachments
while (attachmentsEnumerator.MoveNext())
{
    AttachmentEnumeratorResult res = attachmentsEnumerator.Current;

    AttachmentDetails attachmentDetails = res.Details; // attachment details

    Stream attachmentStream = res.Stream; // attachment contents

    // In this case it is a string attachment, that can be decoded back to text 
    var ms = new MemoryStream();
    attachmentStream.CopyTo(ms);
    string decodedStream = Encoding.UTF8.GetString(ms.ToArray());
}

Load a part of an attachment

Use GetRange to load a part of an attachment by document ID and the attachment name.

Syntax

// Returns a range of the attachment by the document id and attachment name.
AttachmentResult GetRange(string documentId, string name, long? from, long? to);

// Returns a range of the attachment by the document id and attachment name.
AttachmentResult GetRange(object entity, string name, long? from, long? to);

Sample

Album album = session.Load<Album>("albums/1");

AttachmentResult attachmentPart = session.Advanced.Attachments.GetRange(
                                            album, "track1.mp3", 101, 200);
Album album = await asyncSession.LoadAsync<Album>("albums/1");

AttachmentResult file1 = await asyncSession.Advanced.Attachments.GetRangeAsync(
                                            album, "track1.mp3", 101, 200);