Angular2导出/下载JSON文件 [英] Angular2 to export/download json file

查看:481
本文介绍了Angular2导出/下载JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular2.我想提供一些功能来下载JSON文件.

I am using angular 2. I want to give functionality to download JSON file.

就像我有res = {bar:foo}的响应一样,那么我想创建一个包含此响应的json文件,可以在按钮/锚定单击上下载该文件.

Like i have response with res = {bar : foo} then i want to create json file which will contain this response which can be download on button/anchor click.

任何帮助将不胜感激.

Any help will be greatly appreciated.

推荐答案

它比预期的要简单.

constructor(private sanitizer: DomSanitizer){}

    generateDownloadJsonUri() {
        var theJSON = JSON.stringify(this.resJsonResponse);
        var uri = this.sanitizer.bypassSecurityTrustUrl("data:text/json;charset=UTF-8," + encodeURIComponent(theJSON));
        this.downloadJsonHref = uri;
    }

包含在模板中

<a class="btn btn-clear" title="Download JSON" [href]="downloadJsonHref" download="download.json"></a>

这篇关于Angular2导出/下载JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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