---
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: "java"
---

# Get Attachments

> Client language: Java. Also available for [C#](https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments.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), [Node.js](https://docs.ravendb.net/7.2/document-extensions/attachments/get-attachments.nodejs.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

```java
AttachmentName[] getNames(Object entity);

boolean exists(String documentId, String name);

CloseableAttachmentResult get(String documentId, String name);

CloseableAttachmentResult get(Object entity, String name);

CloseableAttachmentResult getRevision(String documentId, String name, String changeVector);
```

### Example

```java
try (IDocumentSession session = store.openSession()) {
  Album album = session.load(Album.class, "albums/1");

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

      InputStream inputStream = file1
          .getData();

      AttachmentDetails attachmentDetails = file1.getDetails();
      String name = attachmentDetails.getName();
      String contentType = attachmentDetails.getContentType();
      String hash = attachmentDetails.getHash();
      long size = attachmentDetails.getSize();
      String documentId = attachmentDetails.getDocumentId();
      String changeVector = attachmentDetails.getChangeVector();
  }

  AttachmentName[] attachmentNames = session.advanced().attachments().getNames(album);
  for (AttachmentName attachmentName : attachmentNames) {

      String name = attachmentName.getName();
      String contentType = attachmentName.getContentType();
      String hash = attachmentName.getHash();
      long size = attachmentName.getSize();
  }

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

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

This operation is used to get an attachment from a document.

#### Syntax

```java
GetAttachmentOperation(String documentId, String name, AttachmentType type, String changeVector)
```

```java
public class CloseableAttachmentResult implements AutoCloseable {
  private AttachmentDetails details;
  private CloseableHttpResponse response;

  public InputStream getData() throws IOException {
      return response.getEntity().getContent();
  }

  public AttachmentDetails getDetails() {
      return details;
  }
}

public class AttachmentDetails extends Foo.AttachmentName {
  private String changeVector;
  private String documentId;

  public String getChangeVector() {
      return changeVector;
  }

  public void setChangeVector(String changeVector) {
      this.changeVector = changeVector;
  }

  public String getDocumentId() {
      return documentId;
  }

  public void setDocumentId(String documentId) {
      this.documentId = documentId;
  }
}

public class AttachmentName {
  private String name;
  private String hash;
  private String contentType;
  private long size;

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  public String getHash() {
      return hash;
  }

  public void setHash(String hash) {
      this.hash = hash;
  }

  public String getContentType() {
      return contentType;
  }

  public void setContentType(String contentType) {
      this.contentType = contentType;
  }

  public long getSize() {
      return size;
  }

  public void setSize(long size) {
      this.size = size;
  }
}
```

| Parameters | | |
| - | - | - |
| **documentId** | String | ID of a document which will contain an attachment |
| **name** | String | Name of an attachment |
| **type** | AttachmentType | 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 | |
| - | - |
| **Stream** | InputStream containing an attachment |
| **ChangeVector** | Change vector of document |
| **DocumentId** | ID of document |
| **Name** | Name of attachment |
| **Hash** | Hash of attachment |
| **ContentType** | MIME content type of an attachment |
| **Size** | Size of attachment |

### Example

```java
store.operations().send(
  new GetAttachmentOperation("orders/1-A", "invoice.pdf", AttachmentType.DOCUMENT, null));
```
