---
title: "How can Download uploaded files in MVC ?"  
description: "How can Download uploaded files in MVC ?"  
author: "Anonymous User"  
published: 2018-08-02  
updated: 2023-06-18  
canonical: https://www.mindstick.com/forum/34650/how-can-download-uploaded-files-in-mvc  
category: "asp.net mvc"  
tags: ["c#", "asp.net mvc", "mvc", "mvc4"]  
reading_time: 3 minutes  

---

# How can Download uploaded files in MVC ?

Use [Simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) [C# code](https://www.mindstick.com/forum/403/how-to-export-data-in-excel-file-from-sql-server-using-c-sharp-code) for [Download](https://www.mindstick.com/articles/188403/website-to-download-photos-from-instagram) [operation](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp) in MVC.

Don't use [ajax](https://www.mindstick.com/articles/1541/check-availability-of-user-name-or-email-using-asp-dot-net-ajax-and-jquery) [script](https://www.mindstick.com/interview/477/how-can-i-execute-a-php-script-using-command-line) for downloads.

## Replies

### Reply by Aryan Kumar

Sure, here are the steps on how to download uploaded files in MVC:

1. Create a controller that handles the download request.
2. Create a view that renders the download link.
3. Implement the download logic in the controller.
4. Configure the MVC routing to map the download request to the controller action.

Here is an example of how to download an uploaded file in MVC:

C#

```plaintext
public class FileController : Controller
{
  public ActionResult Download(string fileName)
  {
    // Get the file from the file system.
    var file = HttpContext.Current.Server.MapPath("~/Uploads/" + fileName);

    // Create a file stream.
    var stream = new FileStream(file, FileMode.Open, FileAccess.Read);

    // Create a file result.
    var result = File(stream, ContentType.ApplicationOctetStream, fileName);

    // Return the file result.
    return result;
  }
}
```

This [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) will create a controller action that can be used to download an uploaded file. The action takes a file name as a parameter and returns a file result. The file result will contain the file stream and the content type of the file.

The view that renders the download link should have a link that points to the controller action. The link should have the file name as the href attribute. For example, the following code shows how to render a download link in a view:

HTML

```plaintext
<a href="/FileController/Download?fileName=myfile.pdf">Download</a>
```

This code will render a link that points to the Download action in the FileController controller. The href attribute of the link will contain the file name myfile.pdf.

The MVC routing needs to be configured to map the download request to the controller action. The following code shows how to configure the MVC routing:

Code snippet

```plaintext
routes.MapRoute(
    name: "DownloadFile",
    url: "/Download/{fileName}",
    defaults: new { controller = "FileController", action = "Download" });
```

This code will map the /Download/{fileName} URL to the Download action in the FileController controller. The fileName parameter will be passed to the controller action as a parameter.

I hope this helps! Let me know if you have other questions.

### Reply by Anonymous User

4). Add a Download button (or Link button) in IndexView Page for download saved files. For this action, we can define only the ActionMethod for download.

## ActionMethod for Download

```
//---------------Download Files-------------//

        public ActionResult Downloads(int customerID)
        {
            Customer model = AP.Customers.FirstOrDefault(x => x.CustomerID == customerID);

            if (model != null)
            {
                if (System.IO.File.Exists(Server.MapPath(resolveVirtual(model.UploadFiles))))
                {
                    string fileName = Path.GetFileName(resolveVirtual(model.UploadFiles));
                    byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(resolveVirtual(model.UploadFiles)).ToString());
                    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
                }
            }

            return null;
        }

        public static string resolveVirtual(string physicalPath)
        {
            string url = physicalPath.Substring(System.Web.HttpContext.Current.Server.MapPath("~").Length).Replace('\\', '/').Insert(0, "~/");
            return (url);
        }
```

5). Work on IndexViewPage.

```
@using (Html.BeginForm("DownloadFile", "Controller", FormMethod.Get)){}
```

![How can Download uploaded files in MVC ?](https://www.mindstick.com/mindstickforums/6ad0e85d-05fc-42eb-aff1-242695d30582/images/b3d1b653-22ad-4b9d-9f5b-0e330b89aaa2.png)

\


---

Original Source: https://www.mindstick.com/forum/34650/how-can-download-uploaded-files-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
