简体   繁体   English

从 Angular 应用程序中的资产下载文件

[英]download a file from assets in Angular app

I have a sample file stored in the assets folder of my Angular App.我有一个示例文件存储在我的 Angular 应用程序的资产文件夹中。 I use the href link to directly save the file to desktop from the browser.我使用 href 链接直接将文件从浏览器保存到桌面。

<a href="/assets/sample.csv" target="_self" download>

However, this works fine when the app is running on localhost:42xx server.但是,当应用程序在 localhost:42xx 服务器上运行时,这可以正常工作。 But after using ng build and deploying it in an external domain it downloads the file but the file contains the html script of my app, where in reality the file present in the assets folder actually contains some random data.但是在使用ng build并将其部署在外部域中后,它会下载文件,但该文件包含我的应用程序的 html 脚本,实际上资产文件夹中的文件实际上包含一些随机数据。 I have no clue why it works on localhost correctly but does not work on an external domain.我不知道为什么它可以在 localhost 上正常工作但在外部域上不起作用。 Thanks in advance提前致谢

You can try this你可以试试这个

<a href="{{ url }}" target="_blank" >Trusted URL</a>

Demo演示

Also you can do it from controller, for example:您也可以从控制器执行此操作,例如:

const path = "https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv";
window.open(path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM