In this article I will explain with an example, how to display list of files from Folder (Directory) in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 

Files Folder (Directory) Location

The files are located inside the Files Folder (Directory) of ASP.Net MVC project.
ASP.Net MVC: Display List of Files from Folder (Directory)
 
 

Model

The Model class consist of following property.
public class FileModel
{
    public string FileName { getset; }
}
 
 

Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, the path of all files in the Files Folder (Directory) is fetched using the GetFiles method of the Directory class and stored into a String Array.
Then, each path is added as new object to the Generic List collection of FileModel class object which is returned to the View.
 

Action method for handling POST operation

This Action method gets called when ActionLink is clicked.
Note: The following Action method performs File Download and hence the return type is set to FileResult.
 
This Action method accepts the name of the file which will be downloaded as parameter.
Then, the BYTE Array is determined using ReadAllBytes method of the File class.
Finally, the BYTE array along with the content type and file name is passed as parameter to File function which downloads the file.
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

HTML Markup

Inside the View, the FileModel class is declared as Generic List collection which specifies that it will be available as a Collection.

Displaying the Files

For displaying the files, an HTML Table is used. A FOR EACH 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 created using Html.ActionLink Helper method for downloading the File.
When the Download link is clicked, the ActionLink calls the DownloadFile Action method which initiates the File download operation.
@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 border="0" cellpadding="0" cellspacing="0">
        <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



Other available versions