Downloading one file at a time

DikongPrigm 61 Reputation points
2022-10-20T23:48:50.933+00:00

How do you guys usually approach this. I'm trying to request a file from the server. The server will then process the file for me and it may take some time to process the file I am requesting for. While the server still process the file, I might want to request another one. This will then enque the request after the previous one.

Let me know your thoughts.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,559 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,928 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,336 Reputation points Microsoft Vendor
    2022-10-21T05:53:35.06+00:00

    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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.