我正在使用 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/