Download File in MVC 4

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.   
  6. namespace FileDownloadInMVC4.Models  
  7. {  
  8.     public class DownLoadFileInformation  
  9.     {  
  10.         public int FileId { getset; }  
  11.         public string FileName { getset; }  
  12.         public string FilePath { getset; }  
  13.     }  
  14. }  
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.   
  8. namespace FileDownloadInMVC4.Models  
  9. {  
  10.     public class DownloadFiles  
  11.     {  
  12.         public List<DownLoadFileInformation> GetFiles()  
  13.         {  
  14.             List<DownLoadFileInformation> lstFiles = new List<DownLoadFileInformation>();  
  15.             DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/MyFiles"));  
  16.   
  17.             int i = 0;  
  18.             foreach (var item in dirInfo.GetFiles())  
  19.             {  
  20.                 lstFiles.Add(new DownLoadFileInformation()  
  21.                 {  
  22.   
  23.                     FileId = i + 1,  
  24.                     FileName = item.Name,  
  25.                     FilePath = dirInfo.FullName + @"\" + item.Name  
  26.                 });  
  27.                 i = i + 1;  
  28.             }  
  29.             return lstFiles;  
  30.         }  
  31.     }  
  32. }  
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.   
  8. namespace FileDownloadInMVC4.Controllers  
  9. {  
  10.     public class FileProcessController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /FileProcess/  
  14.   
  15.         DownloadFiles obj;  
  16.         public FileProcessController()  
  17.         {  
  18.             obj = new DownloadFiles();  
  19.         }  
  20.   
  21.         public ActionResult Index()  
  22.         {  
  23.             var filesCollection = obj.GetFiles();  
  24.             return View(filesCollection);  
  25.         }  
  26.   
  27.         public FileResult Download(string FileID)  
  28.         {  
  29.             int CurrentFileID = Convert.ToInt32(FileID);  
  30.             var filesCol = obj.GetFiles();  
  31.             string CurrentFileName = (from fls in filesCol  
  32.                                       where fls.FileId == CurrentFileID  
  33.                                       select fls.FilePath).First();  
  34.   
  35.             string contentType = string.Empty;  
  36.   
  37.             if (CurrentFileName.Contains(".pdf"))  
  38.             {  
  39.                 contentType = "application/pdf";  
  40.             }  
  41.   
  42.             else if (CurrentFileName.Contains(".docx"))  
  43.             {  
  44.                 contentType = "application/docx";  
  45.             }  
  46.             return File(CurrentFileName, contentType, CurrentFileName);  
  47.         }  
  48.     }  
  49. }  
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

 

Up Next
    Ebook Download
    View all
    Learn
    View all