单击按钮时,从资产文件夹下载文件 [英] Download a file from asset folder when clicking on a button

查看:65
本文介绍了单击按钮时,从资产文件夹下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个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();

这篇关于单击按钮时,从资产文件夹下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆