Rus*_*lva 7 angular angular5 angular6
我有一个网址,例如:abc.net/files/test.ino 要求是通过角度为 5 或 6 的按钮单击事件下载 .INO 文件
HUS*_*AIN 16
您可以创建一个锚标签来下载按钮点击事件上的文件
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();
}
现在从你的按钮调用这个函数
<button (click)="downloadMyFile()">download File<button>
添加到 Hussains 答案,但只是使用 Renderer2 进行,因为不建议直接使用文档。
import { Renderer2 } from '@angular/core'
export class SomeComponent {
   constructor(private renderer: Renderer2) {}
   
   downloadFile() {
      const link = this.renderer.createElement('a');
      link.setAttribute('target', '_blank');
      link.setAttribute('href', 'abc.net/files/test.ino');
      link.setAttribute('download', `products.csv`);
      link.click();
      link.remove();
   }
}