---
title: "Get Attachments"
description: "Retrieve document attachments from RavenDB using the session API with code examples in C#, Java, Python, PHP, and Node.js."
url: "https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments"
version: "7.2"
language: "nodejs"
---

# Get Attachments

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments.md), [Java](https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments.java.md), [Python](https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments.python.md), [PHP](https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments.php.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - This article explains how to retrieve attachments that are already stored in the database.
>
> - In this article
>   - [Get attachment via the session](#get-attachment-via-the-session)
>   - [Get attachment via an operation](#get-attachment-via-an-operation)

## Get attachment via the session {#get-attachment-via-the-session}

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

```javascript
session.advanced.attachments.getNames(entity);

session.advanced.attachments.exists(documentId, name);

session.advanced.attachments.get(documentId, name);

session.advanced.attachments.get(entity, name);

session.advanced.attachments.getRevision(documentId, name, changeVector);
```

| Parameters | | |
| - | - | - |
| **entity** or **documentId** | object or string | instance of the entity or the entity ID |
| **name** | string | attachment name |
| **changeVector** | string | change vector for revision identification |

| Return Value | |
| - | - |
| `Promise<AttachmentStreamResult>` | Promise resolving to a Readable for attachment content |

### Example

```javascript
const album = await session.load("albums/1");

const file1 = await session.advanced.attachments.get(album, "001.jpg");
const file2 = await session.advanced.attachments.get("albums/1", "002.jpg");

const inputStream = file1.data;

const attachmentDetails = file1.details;
//     { 
//       name: '001.jpg',
//       documentId: 'albums/1',
//       contentType: 'image/jpeg',
//       hash: 'MvUEcrFHSVDts5ZQv2bQ3r9RwtynqnyJzIbNYzu1ZXk=',
//       changeVector: '"A:3-K5TR36dafUC98AItzIa6ow"',
//       size: 25793 
//     }

const attachmentNames = await session.advanced.attachments.getNames(album);
for (const attachmentName of attachmentNames) {
  const name = attachmentName.name;
  const contentType = attachmentName.contentType;
  const hash = attachmentName.hash;
  const size = attachmentName.size;
}

const exists = session.advanced.attachments.exists("albums/1", "003.jpg");
// true
```

## Get attachment via an operation {#get-attachment-via-an-operation}

### Get attachment example

```javascript
// Define the get attachment operation
const getAttachmentOp = new GetAttachmentOperation("employees/1-A", "attachmentName.txt", "Document", null);

// Execute the operation by passing it to operations.send
const attachmentResult = await documentStore.operations.send(getAttachmentOp);

// Retrieve attachment content:
attachmentResult.data
  .pipe(fs.createWriteStream("attachment"))
  .on("finish", () => {
      fs.readFile("attachment", "utf8", (err, data) => {
          if (err) {
              console.error("Error reading file:", err);
              return;
          }
          console.log("Content of attachment:", data);
          next();
      });
  });
```

### Syntax

```javascript
const getAttachmentOp = new GetAttachmentOperation(documentId, name, type, changeVector);
```

| Parameter | Type | Description |
| - | - | - |
| **documentId** | `string` | Document ID that contains the attachment. |
| **name** | `string` | Name of attachment to get. |
| **type** | `string` | Specify whether getting an attachment from a document or from a revision.<br />(`"Document"` or `"Revision"`). |
| **changeVector** | `string` | The ChangeVector of the document or the revision to which the attachment belongs.<br />Mandatory when getting an attachment from a revision.<br />Used for concurrency checks (use `null` to skip the check). |

| Return Value of `store.operations.send(getAttachmentOp)` | |
| - | - |
| `AttachmentResult` | An instance of class `AttachmentResult` |

```javascript
class AttachmentResult {
  data;    // Stream containing the attachment content
  details; // The AttachmentDetails object
}

// The AttachmentDetails object:
// =============================
{
  // Change vector of the document that contains the attachment
  changeVector; // string

  // ID of the document that contains the attachment
  documentId?; // string
  
  // Name of attachment
  name; // string;
  
  // Hash of attachment
  hash; // string;
  
  // Content type of attachment
  contentType; // string
  
  // Size of attachment
  size; // number
}
```
