在angular5/6中单击按钮时如何从url下载文件 [英] How to download a file from a url when a button is clicked in angular5 / 6
本文介绍了在angular5/6中单击按钮时如何从url下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网址,例如:abc.net/files/test.ino要求是通过 angular 5 或 6 的按钮点击事件下载 .INO 文件
I have a url for ex: abc.net/files/test.ino The requirement is to download an .INO file through a button click event in angular 5 or 6
推荐答案
你可以创建一个锚标签来在按钮点击事件时下载文件
you can create an anchor tag to download the file on button click event
downloadMyFile(){
const link = document.createElement('a');
link.setAttribute('target', '_blank');
link.setAttribute('href', 'abc.net/files/test.ino');
link.setAttribute('download', `products.csv`);
document.body.appendChild(link);
link.click();
link.remove();
}
现在从你的按钮调用这个函数
now call this function from your button
<button (click)="downloadMyFile()">download File<button>
这篇关于在angular5/6中单击按钮时如何从url下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文