Hi @DikongPrigm ,
I use an API endpoint to return a file, then I use the <a>
tag to display the download button, and after clicking the download button, the server side process the file, after that it downloads the file. By using this method, after clicking the download button, I don't need to wait for the download finished, I can click the download button multiple times, then it will download the file one by one.
Refer to the following sample code:
To download file from the file folder
[Route("api/[controller]")]
[ApiController]
public class ToDoController : ControllerBase
{
private readonly IWebHostEnvironment environment;
public ToDoController(IWebHostEnvironment hostEnvironment)
{
environment = hostEnvironment;
}
[HttpGet("download")]
public IActionResult Download()
{
var filepath = Path.Combine(environment.WebRootPath, "images", "Image1.png");
return File(System.IO.File.ReadAllBytes(filepath), "image/png", System.IO.Path.GetFileName(filepath));
}
Then, in the Index view page, we can add a hyperlink to call this API method and download file:
<a href='~/api/Todo/download' download>Download File From Folder</a>
More detail information, see my reply in this thread: Download file in C# .Net Core
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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,
Dillion