Angular2 - Html link to file download routes to home after click

半腔热情 提交于 2019-12-22 09:37:29

问题


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

The files are located under :

[MY_APP_FOLDER]\src\assets\OffertFile

An example of the link href is this:

/assets/OffertFile/test.xlsx

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?

Thanks to support


回答1:


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

HTML docs: 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>


来源:https://stackoverflow.com/questions/43806329/angular2-html-link-to-file-download-routes-to-home-after-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!