angular2 下载保存文本数据
2017-09-05 02:37
253 查看
原文链接:http://blog.csdn.net/holdlg/article/details/54926136
使用Blob转化为文件对象。
创建一个a标签并加入dom,并设置相关参数,其中Blob会转化为链接。
触发a标签的 click 事件,然后销毁a标签。
Component.ts
http://stackoverflow.com/questions/38793859/angular-2-downloading-a-file-corrupt-result
需求
要导出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 下载保存文本数据
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 通讯录包含保存文本数据和读取文本数据功能
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- python --网页爬虫,文本处理,科学计算,机器学习,数据挖掘资料+附带工具包下载
- C++中利用vector来处理文本中保存的数据
- 收藏——Java导出数据到excel保存在服务器及客户端下载
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 质数保存在文本中,有上万个用空格隔开的数据,有数十个文件,寻找文件夹中所有质数文件的回文数等
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 42节讲数据保存到文本!
- Tip:如何指定浏览器下载并保存动态生成的数据时对话框里的默认文件名
- 将List<Map<String,String>>中的数据输出保存到TXT文本文件中
- C++中利用vector来处理文本中保存的数据
- php导出csv数据在浏览器中输出提供下载或保存到文件的示例
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- springMvc 中 Excel批量数据上传,利用POI解析数据保存入数据库 及模板文件下载
- 自动化爬取淘宝数据--(保存到文本中)