In this article I will explain with an example, how to display list of files from Folder (Directory) in ASP.Net Core (.Net Core) MVC.
Files Folder (Directory) Location
The files are located inside the Files Folder (Directory) of wwwroot Folder (Directory).
Model
The Model class consist of following property.
public class FileModel
{
public string FileName { get; set; }
}
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) which is present inside the wwwroot Folder (explained earlier) is fetched using the IWebHostEnvironment interface into a String Array using the GetFiles method of the Directory class.
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
{
private IWebHostEnvironment Environment;
public HomeController(IWebHostEnvironment _environment)
{
this.Environment = _environment;
}
public IActionResult Index()
{
//Fetch all files in the Folder (Directory).
string[] filePaths = Directory.GetFiles(Path.Combine(this.Environment.WebRootPath, "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 = Path.Combine(this.Environment.WebRootPath, "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 MVC_Core_File_Download.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" cellspacing="0" cellpadding="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
Downloads