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?
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Could you please let us know to download pdf/excel file from server location to client without using javascript?
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?
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>
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
Hi @Anantham.S ,
Actually, it is same as downloading a file in a razor page app. You can implement it with the below steps:
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:
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