单击按钮时,从资产文件夹下载文件

单击按钮时,从资产文件夹下载文件

问题描述:

我正在做一个angular2项目,我在Assets文件夹中有一个文件,并且创建了一个在运行应用程序时下载文件的按钮.

I am working on an angular2 project, I have a file in assets folder and I have created a button to download the file while running the app.

我看到上述问题有很多解决方案,所以我很困惑.你能帮忙吗?

I see there are quite a many solutions to the above problem so i got confused. Can you please help.

<button pButton type="button" (click)="f1()" label="Download Sample Defaults 
XML File"></button>

我需要f1()的代码,当单击上面的按钮时,该代码可以帮助我将文件下载到我的下载"文件夹中.帮助表示赞赏.谢谢

I need code for f1() which can help me download the file to my Download folder when clicking on above button. Help appreciated. Thanks

您可以将锚元素的样式设置为类似于按钮,并将其href设置为资产/文件

You can either style an anchor element to look like a button and set it's href to assets/file

<a href="assets/file">Download here</a>

或动态创建锚元素,将其设置为href元素,然后单击它.

Or create an anchor element on the fly, set it's href element and click it.

类似的东西:

let link = document.createElement('a');
link.setAttribute('type', 'hidden');
link.href = 'assets/file';
link.download = path;
document.body.appendChild(link);
link.click();
link.remove();