# Creating a Download Command For The FileManager

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Progress Telerik UI for ASP.NET MVC</td>
 </tr>
</table>

## Description

How can I create a Download command in the context menu of the FileManager component for Telerik UI for ASP.NET MVC?

## Creating a Download Command

As of Telerik UI for ASP.NET MVC version R1 2020 SP1, the `kendo.ui.filemanager` namespace exposes the `FileManagerCommand` class that you can extend and implement a download command.

Include [Font Icons](https://www.telerik.com/design-system/docs/foundation/iconography/font-icons/#usage) for [`SpriteCssClass`](https://docs.telerik.com/kendo-ui/api/ui/filemanager/configuration/contextmenu.items.spritecssclass) by referencing the following link extracted from [unpkg](https://unpkg.com/):

```html
<link rel="stylesheet" href="http://unpkg.com/%40progress/kendo-font-icons/dist/index.css" rel="stylesheet" type="text/css" />
```

Initialize the Telerik UI for ASP.NET MVC FileManager and define a Download command for the Context Menu.

```Razor View
  @(Html.Kendo().FileManager().Name("filemanager")
  ...
        .ContextMenu(context => context.Items(items =>
        {
            items.Add("download").Command("DownloadCommand").Text("Download").SpriteCssClass("k-icon k-font-icon k-i-download");
        }))
    )
```

Extend the `FileManagerCommand` class and pass the selected file path to a controller action method.

```javascript
  var filemanagerNS = kendo.ui.filemanager;

      filemanagerNS.commands.DownloadCommand = filemanagerNS.FileManagerCommand.extend({
          exec: function(){
              var that = this,
                  filemanager = that.filemanager, // get the kendo.ui.FileManager instance
                  options = that.options, // get the options passed through the tool
                  target = options.target // options.target is available only when command is executed from the context menu
                  selectedFiles = filemanager.getSelected(); // get the selected files

                  window.location = '/FileManager/Download?path=' + selectedFiles[0].path;

          }
      });
```

On the server-side, implement a server-side action to return the file for download. The example below extends the [File Manager demo for Telerik UI for ASP.NET MVC](https://demos.telerik.com/aspnet-mvc/filemanager)

```C# Controller
  [HttpGet]
  public FileResult Download(string path)
  {
      var virtualPath = "~/Content/UserFiles/Folders/" + path;
      var filePath = HostingEnvironment.MapPath(virtualPath);
      FileInfo file = new FileInfo(filePath);

      System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
      {
          FileName = file.Name,
          Inline = false
      };
      Response.Headers.Add("Content-Disposition", cd.ToString());
      Response.Headers.Add("X-Content-Type-Options", "nosniff");

      string contentType = MimeMapping.GetMimeMapping(file.Name);
      var readStream = System.IO.File.ReadAllBytes(filePath);
      return File(readStream, contentType);
  }
```

## More ASP.NET MVC FileManager Resources

* [ASP.NET MVC FileManager Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/filemanager/overview)

* [ASP.NET MVC FileManager Demos](https://demos.telerik.com/aspnet-mvc/filemanager)

* [ASP.NET MVC FileManager Product Page](https://www.telerik.com/aspnet-mvc/file-manager)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the FileManager for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/filemanager)
* [Server-Side API Reference of the FileManager for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/filemanager)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
