I'm assuming, you are working with ASP.NET Core application, to download a file from the wwwroot folder when a button is clicked, you can use the PhysicalFile method and return a FileContentResult from the controller.
Here's an example:
using Microsoft.AspNetCore.Mvc;
using System.IO;
public IActionResult DownloadSingleFile()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Book1.xlsx");
if (System.IO.File.Exists(path))
{
var memory = new MemoryStream();
using (var stream = new FileStream(path, FileMode.Open))
{
stream.CopyTo(memory);
}
memory.Position = 0;
return File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Path.GetFileName(path));
}
else
{
return NotFound();
}
}
This method reads the file from the wwwroot folder, copies it to a memory stream, and then returns it as a file download. The File method is used to return the file content as an application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (MIME type for Excel files) to the client.
Make sure you have configured your routing properly to ensure the DownloadSingleFile method is correctly mapped to the corresponding URL.