angular2 下载保存文本数据
2017-02-08 14:03
155 查看
需求
要导出API提供的数据,保存到本地。思路
获取API数据。使用Blob转化为文件对象。
创建一个a标签并加入dom,并设置相关参数,其中Blob会转化为链接。
触发a标签的 click 事件,然后销毁a标签。
源码
Service.tsdownloadService(): any { let link = `${RootUrl}/data/all`; return this.http.get(link) .map(res => new Blob([res.text()],{ type: 'application/json' })) .catch(this.handleError); }
Component.ts
downloadComponent(): void { var link = document.createElement("a"); this._listService.downloadService() .subscribe(data => { // Blob转化为链接 link.setAttribute("href", window.URL.createObjectURL(data)); link.setAttribute("download", 'filename.json'); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } ); }
参考链接
http://stackoverflow.com/questions/35138424/how-do-i-download-a-file-with-angular2http://stackoverflow.com/questions/38793859/angular-2-downloading-a-file-corrupt-result
相关文章推荐
- angular2 下载保存文本数据
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- Tip:如何指定浏览器下载并保存动态生成的数据时对话框里的默认文件名
- 关于Excel操作编写的一个软件设计构思案例[连载] --辅助功能:补全缺少数据、树定位、文本读与保存
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
- js读取本地磁盘文本文件并保存为JSON数据(有格式的文本)
- SRTUTS + ITEXT 创建日志数据的保存下载
- 用jxl 从jsp页面导出数据到excel表格并下载保存
- C++中利用vector来处理文本中保存的数据
- 从hibernate 向 sql server 保存 大数据 文本 或图片
- python从ftp下载数据保存实例
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- 收藏——Java导出数据到excel保存在服务器及客户端下载
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- [导入]Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载)
- 收藏——Java导出数据到excel保存在服务器及客户端下载
- PHP再学习3——RT Thread + LwIP提交表单,文本保存数据