javascript - Angular2导出/下载json文件

标签 javascript json angular download export

我正在使用 angular 2。我想提供下载 JSON 文件的功能。

就像我有 res = {bar : foo} 的响应,然后我想创建 json 文件,其中将包含可以在按钮/ anchor 击时下载的响应。

任何帮助将不胜感激。

最佳答案

它比预期的要简单。

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>

关于javascript - Angular2导出/下载json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360665/

相关文章:

php - while-loop div 没有反应

javascript - 使用JS将毫秒转换为SRT时间

ios - 将存储在 NSDictionary 中的 JSON 数据中的应用程序名称存储在 NSArray 中

javascript - 通过 chrome 扩展检查 cookie 是否存在

Angular 属性选择器 : Output Binding Not Working

angular - 以编程方式删除选择上的焦点/模糊(Angular6 Material )

javascript - 如何将 Vanilla JS 添加到我的 React 应用程序中以创建图像模态弹出窗口?

json - Swift 中的 Realm DB 和对象映射器 (Json)

angular - 如何使用 ngx-leaflet( Angular 方式)创建矢量图 block map ?

javascript - 这个作用域/闭包什么时候在 javaScript 中被垃圾收集?