freddoo
freddoo

Reputation: 6858

in blazor wasm how to download a file using the a tag without changing the current url

I have a template on a page.

I want the users to be able to download it without changing the url of the current page.

My code is really simple

<a href="@FileLink()" download>@FileName</a>

FileLink() is a function that returns the relative file path and name.

It all works fine except for the fact that the url of the page get changed for what FileLink() returns followed by "?"

Upvotes: 1

Views: 1042

Answers (1)

freddoo
freddoo

Reputation: 6858

I have finally found the solution on the web.

Simple solution :

"Method 2 - make tag with download attribute, and ... target="_top" attribute! To avoid the interception of URL navigation by Blazor, we can use a hack.

The hack is, adding target="_top" attribute to the tag. download

Blazor doesn't intercept URL navigation in some cases, and adding target="_top" attribute is one of those cases.

This solution is very simple, and works fine as expected, as perfect."

https://dev.to/j_sakamoto/implement-the-download-file-feature-on-a-blazor-webassembly-app-2f8p

So my final tag looks like

<a href="@FileLink()" download="@FileName" target="_top">@FileName</a>

Upvotes: 3

Related Questions