在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>