Angular2 - 单击后指向文件下载路径的 Html 链接 [英] Angular2 - Html link to file download routes to home after click

查看:30
本文介绍了Angular2 - 单击后指向文件下载路径的 Html 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Angular2 应用程序,它公开了一些文件的直接下载链接.

I have an Angular2 application that expose some direct download links to files.

文件位于:

[MY_APP_FOLDER]\src\assets\OffertFile

链接 href 的示例如下:

An example of the link href is this:

/assets/OffertFile/test.xlsx

component.html 链接示例如下:

The component.html link example is this:

<a href="/assets/OffertFile/test.xlsx" target="_self">
      <i class="fa fa-download fa-2x text-primary" aria-hidden="true"></i>
</a>

链接有效,我可以下载文件.问题是,一旦我点击链接,应用程序就会路由到家...

The link works, I can download the file. The problem is that once I click the link, the application routes to home...

我怎样才能避免这种情况?

How can I avoid this?

感谢支持

推荐答案

请将 download 属性添加到 标签.这将阻止路由更改并告诉浏览器这是一个下载链接:

Please add the download attribute to the <a> tag. This will prevent rout changes and tell the browser it is a download link:

HTML 文档:https://www.w3schools.com/tags/tag_a.asp

<a href="/assets/OffertFile/test.xlsx" target="_self" download>
      <i class="fa fa-download fa-2x text-primary" aria-hidden="true"></i>
</a>

这篇关于Angular2 - 单击后指向文件下载路径的 Html 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆