Skip to main content

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

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

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