question

AnanthamS-6846 avatar image
0 Votes"
AnanthamS-6846 asked JorgeArana-3700 commented

Blazor Server App - Downlaod files from server

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

dotnet-aspnet-core-blazor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It is incredible for me. Sometimes I think that Blazor has not been made for me. It works fine for everyone, but I get an error message on the line "return File(fileBytes," because it doesn't recognize "File".

0 Votes 0 ·
JiadongMeng-MSFT avatar image
6 Votes"
JiadongMeng-MSFT answered MazamaAWESSO-7631 commented

Hi @AnanthamS-6846,

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



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you Jiadong. This helped me :)

Best regards
Mark

0 Votes 0 ·

Thanks you for the solution.
iam just starting blazor for a project and it really helped me.

Thanks

0 Votes 0 ·
Mario1278 avatar image
0 Votes"
Mario1278 answered

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

@page "/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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

moudey avatar image
2 Votes"
moudey answered

Hi @Mario1278 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>


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GayanWijesinghe-8503 avatar image
0 Votes"
GayanWijesinghe-8503 answered GayanWijesinghe-8503 commented

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

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

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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

The browser can download one file at a time.



1 Vote 1 ·

I was thinking about repeating the call with a list of file names, it didn't work because force reload triggered on the first one.

I opted for zipping up the files before downloading.

0 Votes 0 ·

Try again with
NavigationManager.NavigateTo("/download/file.zip", true)

1 Vote 1 ·

Thanks, I figured that I was missing the force reload option.
It works now.

0 Votes 0 ·