This article provides a sample showing how to download files from a directory in MVC 4.

Create a new ASP.NET MVC 4 Application as in the following:

blank MVC application
Image 1

internet application
Image 2

Now, right-click on the Model Folder then select Add New Item -> Add a New Class.

class
Image 3

In DownLoadFileInformation use the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace FileDownloadInMVC4.Models
  6. {
  7. public class DownLoadFileInformation
  8. {
  9. public int FileId { get; set; }
  10. public string FileName { get; set; }
  11. public string FilePath { get; set; }
  12. }
  13. }
Now, again right-click on Mode then select Add New Item -> Add New Class.

blank class
Image 4

In DownloadFiles.cs use the following code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Hosting;
  7. namespace FileDownloadInMVC4.Models
  8. {
  9. public class DownloadFiles
  10. {
  11. public List<DownLoadFileInformation> GetFiles()
  12. {
  13. List<DownLoadFileInformation> lstFiles = new List<DownLoadFileInformation>();
  14. DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/MyFiles"));
  15. int i = 0;
  16. foreach (var item in dirInfo.GetFiles())
  17. {
  18. lstFiles.Add(new DownLoadFileInformation()
  19. {
  20. FileId = i + 1,
  21. FileName = item.Name,
  22. FilePath = dirInfo.FullName + @"\" + item.Name
  23. });
  24. i = i + 1;
  25. }
  26. return lstFiles;
  27. }
  28. }
  29. }
Now open FileProcessController and do the following code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using FileDownloadInMVC4.Models;
  7. namespace FileDownloadInMVC4.Controllers
  8. {
  9. public class FileProcessController : Controller
  10. {
  11. //
  12. // GET: /FileProcess/
  13. DownloadFiles obj;
  14. public FileProcessController()
  15. {
  16. obj = new DownloadFiles();
  17. }
  18. public ActionResult Index()
  19. {
  20. var filesCollection = obj.GetFiles();
  21. return View(filesCollection);
  22. }
  23. public FileResult Download(string FileID)
  24. {
  25. int CurrentFileID = Convert.ToInt32(FileID);
  26. var filesCol = obj.GetFiles();
  27. string CurrentFileName = (from fls in filesCol
  28. where fls.FileId == CurrentFileID
  29. select fls.FilePath).First();
  30. string contentType = string.Empty;
  31. if (CurrentFileName.Contains(".pdf"))
  32. {
  33. contentType = "application/pdf";
  34. }
  35. else if (CurrentFileName.Contains(".docx"))
  36. {
  37. contentType = "application/docx";
  38. }
  39. return File(CurrentFileName, contentType, CurrentFileName);
  40. }
  41. }
  42. }
Now right-click on the Index Action then select Add A View.

model class
Image 6

Now run the application:

file download in MVC4
Image 7

Now click on any Download link.

upload new file
Image 8