In this article I will explain with an example, how to display List of files from Folder (Directory) on Server’s Disk in View in ASP.Net MVC Razor.
The Files can also be downloaded from Folder (Directory) in ASP.Net MVC Razor.
 
 
Folder (Directory) Location
The Folder (Directory) is located within the Project. Below is the screenshot of the Solution Explorer window where you can see the Folder (Directory) i.e. Files and it containing three files.
ASP.Net MVC: Display List of Files from Folder (Directory)
 
 
Model
Following is a Model class named FileModel with one property i.e. FileName.
public class FileModel
{
    public string FileName { get; set; }
}
 
 
Namespaces
You will need to import the following namespace.
using System.IO;
 
 
Controller
The Controller consists of two Action methods.
Action method for handling File Display operation
Inside this Action method, the list of all files in the Files Folder (Directory) is fetched into a String Array using the GetFiles method of the Directory class.
Later, the values of the Array are copied to a Generic List collection consisting objects of FileModel class and then returned to the View.
 
Action method for handling File Download operation
This action method handles the File Download operation and when the ActionLink is clicked, the name of the File to be downloaded is sent to this method.
Note: The following Action method performs File Download and hence the return type is set to FileResult.
 
First, the File is read as Binary Data into a Byte Array object using the ReadAllBytes method of the File class.
And then the Byte Array object is sent for download using the File function.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        //Fetch all files in the Folder (Directory).
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
 
        //Copy File names to Model collection.
        List<FileModel> files = new List<FileModel>();
        foreach (string filePath in filePaths)
        {
            files.Add(new FileModel { FileName = Path.GetFileName(filePath) });
        }
 
        return View(files);
    }
 
    public FileResult DownloadFile(string fileName)
    {
        //Build the File Path.
        string path = Server.MapPath("~/Files/") + fileName;
 
        //Read the File data into Byte Array.
        byte[] bytes = System.IO.File.ReadAllBytes(path);
 
        //Send the File to Download.
        return File(bytes, "application/octet-stream", fileName);
    }
}
 
 
View
Inside the View, the FileModel class is declared as List which specifies that it will be available as a Collection.
Displaying the Files
For displaying the files, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the File records.
 
Downloading the File
The HTML Table contains an ActionLink for downloading the File.
When the Download link is clicked, the ActionLink calls the DownloadFile Action method and the File is downloaded.
@using File_Folder_Download_MVC.Models
@model List<FileModel>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table>
        <tr>
            <th>File Name</th>
            <th></th>
        </tr>
        @foreach (FileModel file in Model)
        {
            <tr>
                <td>@file.FileName</td>
                <td>@Html.ActionLink("Download", "DownloadFile", new { fileName = file.FileName })</td>
            </tr>
        }
    </table>
</body>
</html>
 
 
Screenshot
ASP.Net MVC: Display List of Files from Folder (Directory)
 
 
Downloads