# Handling media files and download it

## Handle when the user sends some media file

Use `@OnMedia`, `@OnPhoto`, `@OnVideo`, `@OnAudio` decorators to handle when the user sends some media file. You can read more about this [here](/nestgram/handling-updates/handling-other-updates.md#allowed-updates)

## Download sent media

Use the `.saveFile` method in the **Answer class** to do this and pass the path to it. You can use this method to download any sent media *(e.g. audio, video, photo)*. Below, you can see how to download the photo

{% hint style="info" %}
`.saveFile` method takes the path in which you want to save file as argument

You can read more about `Answer` class [here](/nestgram/nestgram-features/answer-class.md)
{% endhint %}

#### .saveFile method take arguments:

<table><thead><tr><th data-type="number">Argument</th><th>Description</th><th>Required</th></tr></thead><tbody><tr><td>1</td><td>Path in which you want to save file</td><td><strong>Required</strong></td></tr><tr><td>2</td><td>File id you want to save</td><td>Optional. Current file id by default (downloads media file sent by the user)</td></tr></tbody></table>

{% code title="app.controller.ts" %}

```typescript
import { Answ, Answer, OnPhoto, Controller } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

@Controller()
export class AppController {
  constructor(private readonly appService?: AppService) {}

  @OnPhoto()
  async savePhoto(@Answ() answer: Answer): Promise<string> {
    await answer.saveFile(path.resolve(__dirname, 'image.png'));
    return 'Media file downloaded';
  }
}
```

{% endcode %}

## Download sent media using SaveFile class

You can also use SaveFile class-method to download media sent by the user. We recommend using  this class-method if possible

#### SaveFile class-method take arguments:

<table><thead><tr><th data-type="number">Argument</th><th>Description</th><th>Required</th></tr></thead><tbody><tr><td>1</td><td>Path in which you want to save file</td><td><strong>Required</strong></td></tr><tr><td>2</td><td>File id you want to save</td><td>Optional. Current file id by default (downloads media file sent by the user)</td></tr></tbody></table>

{% code title="app.controller.ts" %}

```typescript
import { Controller, SaveFile, OnPhoto } from 'nestgram';
import { AppService } from './app.service';
import * as path from 'path';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @OnPhoto()
  async savePhoto(): Promise<SaveFile> {
    return new SaveFile(path.resolve(__dirname, 'image.png')).next('File saved!');
  }
}
```

{% endcode %}

{% hint style="info" %}
About `.next` syntax you can read [here](/nestgram/nestgram-features/sending-messages-correctly.md#sending-messages-as-answer)
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://degreetpro.gitbook.io/nestgram/handling-updates/handling-media-files-and-download-it.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
