Hi @Anantham.S ,
Actually, it is same as downloading a file in a razor page app. You can implement it with the below steps:
- Add a link to the page: <a class="form-control btn btn-primary" href="/download">Download</a>
- Add a razor page to the Pages folder, name it like "Download"
Download.cshtml:
@page "/download"
@model Namespace.DownloadModel
Download.cshtml.cs:
public class DownloadModel : PageModel
{
private readonly IWebHostEnvironment _env;
public DownloadModel(IWebHostEnvironment env)
{
_env = env;
}
public IActionResult OnGet()
{
var filePath = Path.Combine(_env.WebRootPath, "files", "file1.xlsx");
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, "application/force-download", "file1.xlsx");
}
}
Result:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Jiadong Meng