In this article I will explain with an example, how to download
CSV document from
Database in ASP.Net Core (.Net Core 8).
Database
I have made use of the following table tblFiles with the schema as follow.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Model
The Model class consists of the following properties.
public class FileModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ContentType { get; set; }
public byte[] Data { get; set; }
}
Database Context
The very first step is to create Database Context class.
Inside the class, first inherit the EntityFrameworkCore namespace and then inherit the DbContext class.
Then using Dependency Injection, a Constructor is created DbContextOptions are passed as parameter and also the Constructor of base class i.e. DbContext class is inherited.
A
DbSet Collection property of
FileModel Entity is created, which will be later used for holding the Data fetched from
SQL Server Database Table.
using Download_CSV_Database_Core.Models;
using Microsoft.EntityFrameworkCore;
namespace Download_CSV_Database_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<FileModel> tblFIles { get; set; }
}
}
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, the records are fetched from the
tblFiles Table using
Entity Framework and returned to the View.
Action method for handling POST operation
This Action method is executed when the Download Button is clicked.
Inside this Action method,
fileId is received as parameter and the records are fetched from
tblFiles Table using
Entity Framework based on the
fileId.
Finally, the file is downloaded using File Function.
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
return View(this.Context.tblFIles.ToList());
}
[HttpPost]
public IActionResult DownloadFile(int? fileId)
{
FileModel file = this.Context.tblFIles.Where(f => f.Id == fileId).FirstOrDefault();
return File(file.Data, file.ContentType, file.Name);
}
}
View
HTML Markup
Inside the View, in the very first line the FileModel is declared as IEnumerable which specifies that it will be available as a Collection.
asp--action – Name of the Action. In this case the name is DownloadFile.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Form for Downloading the File
The Form consists of an HTML HiddenField and a Hidden Submit Button.
When any
Download Link inside the HTML Table (Grid) is clicked, then the
DownloadFile JavaScript function is called, which sets the
FileId into the HiddenField and calls the click event of the
Submit button which submits the Form.
When the Form is submitted, the DownloadFile Action method is called, which performs the File download operation.
Displaying the Files
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the tblFiles records.
@using Download_CSV_Database_Core.Models
@model IEnumerable<FileModel>
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-action="DownloadFile" asp-controller="Home">
<input type="hidden" id="hfFileId" name="FileId" />
<input type="submit" id="btnDownload" value="Download" style="display:none" />
</form>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th style="width:120px">File Name</th>
<th style="width:80px"></th>
</tr>
@foreach (var file in Model)
{
<tr>
<td>@file.Name</td>
<td><a href="javascript:;" onclick="DownloadFile(@file.Id)">Download</a></td>
</tr>
}
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function DownloadFile(fileId) {
$("#hfFileId").val(fileId);
$("#btnDownload")[0].click();
};
</script>
</body>
</html>
Screenshot
Downloads