Blazor Server App - Downlaod files from server

Anantham.S 1 Reputation point
2021-01-25T05:36:30.97+00:00

Could you please let us know to download pdf/excel file from server location to client without using javascript?

Developer technologies | .NET | Blazor
{count} votes

4 answers

Sort by: Most helpful
  1. Jiadong Meng - MSFT 131 Reputation points
    2021-01-26T07:26:32.757+00:00

    Hi @Anantham.S ,

    Actually, it is same as downloading a file in a razor page app. You can implement it with the below steps:

    1. Add a link to the page: <a class="form-control btn btn-primary" href="/download">Download</a>
    2. Add a razor page to the Pages folder, name it like "Download" pcSpO.png

    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:

    Hkp7f.gif


    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

    8 people found this answer helpful.

  2. Mahmoud Gomaa 16 Reputation points
    2022-01-09T18:55:53.367+00:00

    Hi @Mario The following example illustrates how to receive a parameter from the page

    @page "{file?}"  
      
    @functions {  
             public IActionResult OnGet(string file)  
             {  
                  if(string.IsNullOrEmpty(file)  
                      return NotFound();  
      
                   byte[] fileBytes = System.IO.File.ReadAllBytes("path-to-dir\" + file);  
                   return File(fileBytes, "application/force-download", file);  
             }  
    }  
    

    <a href="/download/file.zip">download</a>

    2 people found this answer helpful.
    0 comments No comments

  3. Mario 1 Reputation point
    2021-07-23T13:10:39.417+00:00

    Could you give me a hint on how to access routing variables in this example? i.e. I access URL/download/Filename.txt

    How woudl I go about retrieving the string "Filename.txt"? In Razor/Blazor normally it's done with routing variables, by declaring

    @Anonymous "/download/{myvar}" and then declaring / accessing a [Parameter]string Myvar in the code. Obviously this doesn't work (out of the box ) with your code, but I am a bloody beginner in the Razor / Blazor world, and have no idea what to change.
    (ok, I managed to access the HttpContext......Path, and extract the information from there, but it feels wrong :) )

    Also maybe one more question, would it work to create a button somewhere in my Blazor SPA, and in the code for the button, use Navigationmanager to access the download page, like this:
    NavigationManager.NavigateTo("/download/Filename.txt");

    Thanks for any advice you can give,

    Best regards
    Mario

    0 comments No comments

  4. Gayan Wijesinghe 1 Reputation point
    2022-01-31T05:04:11.817+00:00

    Hi @Mahmoud Gomaa , this works in "<a href " but not in NavigationManager.NavigateTo.

    I'm trying to download multiple files on a single button click, any ideas?


Your answer

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