# 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](https://degreetpro.gitbook.io/nestgram/handling-other-updates#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](https://degreetpro.gitbook.io/nestgram/nestgram-features/answer-class)
{% 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](https://degreetpro.gitbook.io/nestgram/nestgram-features/sending-messages-correctly#sending-messages-as-answer)
{% endhint %}
