在 blazor wasm 中如何使用 a 标签下载文件而不改变当前 url
in blazor wasm how to download a file using the a tag without changing the current url
我在页面上有一个模板。
我希望用户能够在不更改当前页面的 url 的情况下下载它。
我的代码真的很简单
<a href="@FileLink()" download>@FileName</a>
FileLink() 是 returns 相对文件路径和名称的函数。
除了页面的 url 更改为 FileLink() returns 后跟“?”
之外,一切正常
您可以只制作一个按钮,然后使用 javascript 下载文件。
我终于在网上找到了解决方案。
简单的解决方案:
"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
所以我的最终标签看起来像
<a href="@FileLink()" download="@FileName" target="_top">@FileName</a>
我在页面上有一个模板。
我希望用户能够在不更改当前页面的 url 的情况下下载它。
我的代码真的很简单
<a href="@FileLink()" download>@FileName</a>
FileLink() 是 returns 相对文件路径和名称的函数。
除了页面的 url 更改为 FileLink() returns 后跟“?”
之外,一切正常您可以只制作一个按钮,然后使用 javascript 下载文件。
我终于在网上找到了解决方案。
简单的解决方案:
"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
所以我的最终标签看起来像
<a href="@FileLink()" download="@FileName" target="_top">@FileName</a>