In this article I will explain with an example, how to download
Video (MP4) from
Database in ASP.Net Core (.Net Core 8) Razor Pages.
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 and 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_MP4_Database_Core_Razor.Models;
using Microsoft.EntityFrameworkCore;
namespace Download_MP4_Database_Core_Razor.Pages
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<FileModel> tblFIles { get; set; }
}
}
Razor PageModel (Code-Behind)
The PageModel consists of following Handler methods.
Inside the Razor PageModel, a Generic Collection of FileModel class public property is created and the DBContext is injected using the DependencyInjection method.
Handler method for handling GET operation
Inside this Handler method, the records are fetched from the
tblFiles Table using
Entity Framework and assigned to the public property of Generic Collection of
FileModel class.
Handler method for handling POST operation
This Handler method is executed when the Download Button is clicked.
Inside this Handler 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 IndexModel : PageModel
{
public List<FileModel> Files { get; set; }
private DBCtx Context { get; }
public IndexModel(DBCtx _context)
{
this.Context = _context;
}
public void OnGet()
{
this.Files = this.Context.tblFIles.ToList();
}
public FileResult OnGetDownloadFile(int fileId)
{
FileModel file = this.Context.tblFIles.Where(f => f.Id == fileId).FirstOrDefault();
return File(file.Data, file.ContentType, file.Name);
}
}
Razor Page (HTML)
HTML Markup
The HTML of Razor Page consists of an HTML Table for displaying following ASP.Net Tag Helpers attributes.
asp-page-handler – Name of the page. In this case the name is DownloadFile.
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.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnGetDownloadFile but here it will be specified as DownloadFile when calling from the Razor HTML Page.
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.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Download_MP4_Database_Core_Razor.Pages.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post">
<input type="hidden" id="hfFileId" name="FileId" />
<input type="submit" id="btnDownload" value="Download" asp-page-handler="DownloadFile" style="display:none" />
</form>
<hr/>
<table id="tblFiles" cellpadding="0" cellspacing="0">
<tr>
<th style="width:50px">File Id</th>
<th style="width:120px">File Name</th>
<th style="width:80px">Download</th>
</tr>
@foreach (var file in Model.Files)
{
<tr>
<td>@file.Id</td>
<td>@file.Name</td>
<td><a href="@Url.Page("Index", "DownloadFile", new { fileId = file.Id })">Download</a></td>
</tr>
}
</table>
</body>
</html>
Screenshot
Downloads