---
title: "Download files in ASP.NET MVC"  
description: "In this article I am explaining how to download files in ASP.NET MVC from the physical drive of the server.  Open studio 2010 and create a new ASP.NET"  
author: "Chris Anderson"  
published: 2011-10-10  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/774/download-files-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 3 minutes  

---

# Download files in ASP.NET MVC

In this article I am explaining how to [download](https://www.mindstick.com/blog/114673/download-hungry-shark-evolution-mod-apk-unlimited-money) [files in ASP.NET](https://www.mindstick.com/interview/34140/what-is-the-use-of-the-program-cs-and-startup-cs-files-in-asp-dot-net-core) MVC from the [physical](https://yourviews.mindstick.com/view/82167/from-physical-to-digital-the-pandemic-acros) drive of the server.\

Open studio 2010 and create a new [ASP.NET MVC](https://www.mindstick.com/articles/1571/start-with-asp-dot-net-mvc-4) 3 [Web Application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about) [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) as seen below:

![Download files in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/1eb71d52-8a21-4edf-bdb6-a934fe9b4bfb/images/a35534b0-b4bc-47a1-9ad2-7b6f0608f6a2.png)

Choose ASPX as the view engine and click OK.

![Download files in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/1eb71d52-8a21-4edf-bdb6-a934fe9b4bfb/images/e20461ca-18b6-41f4-aea5-7b9de8df6099.png)

Add a controller and give the name as HomeController.

![Download files in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/1eb71d52-8a21-4edf-bdb6-a934fe9b4bfb/images/23a1d9b4-3038-4a1f-9c98-9119ca99abc6.png)

Add folder (ImageFile) in Solution Explorer, also add two file in that folder i.e. Koala.jpg and Penguins.jpg.

![Download files in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/1eb71d52-8a21-4edf-bdb6-a934fe9b4bfb/images/64140a13-4ec1-4bcb-b940-5b73fb75eca4.png)

After adding files create a View (Index) as shown below:

```
<html><head runat="server">    <title>Index</title></head><body>    <div>        <table>            <tr>                <td>                    <img src="../../ImageFile/Koala.jpg" width="300" height="300" />
                </td>                <td>                    <img src="../../ImageFile/Penguins.jpg" width="300" height="300" />                </td>            </tr>            <tr>
                <td align="center">                    <a href="/Home/Download/1">Download</a><br />                </td>                <td align="center">                    <a href="/Home/Download/2">Download</a>                </td>            </tr>        </table>    </div></body>
</html>
```

Next you have to create an [Action method](https://www.mindstick.com/forum/155751/define-action-method-in-mvc) in a controller that returns the file according to id passed to the method. Here if the id passed is as 1 then Koala.jpg file will return and if 2 are passed it will return Penguins.jpg

```
using System;using System.Web.Mvc;

namespace DownloadDemo.Controllers{
    public class HomeController : Controller    {        public ActionResult Index()        {            return View();
        }
        public FilePathResult Download(int id)        {
            string fileName = "";            if (id == 1)
                fileName = "Koala.jpg";            if (id == 2)                fileName = "Penguins.jpg";            string path = AppDomain.CurrentDomain.BaseDirectory + "ImageFile/";            return File(path + fileName, "image/jpg", fileName);        }    }
}
```

After creating a [Controller method](https://www.mindstick.com/forum/159704/what-is-the-use-of-the-producesresponsetype-attribute-in-a-dot-net-core-api-controller-method) the image is displayed on the page as follows:

![Download files in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/1eb71d52-8a21-4edf-bdb6-a934fe9b4bfb/images/c014f589-8af3-4e4e-b079-0b061fad5a03.png)

When you click on the Download link the file ready for the download:

![Download files in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/1eb71d52-8a21-4edf-bdb6-a934fe9b4bfb/images/f3e8e56c-e9f3-4e38-ad0a-6edd1d4c318c.png)

Thanks for reading this article.

---

Original Source: https://www.mindstick.com/articles/774/download-files-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
