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

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

问题描述

我正在处理一个 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 设置为 assets/file

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天全站免登陆