angular2 通过get方法请求下载二进制的Excel文件
2017-11-22 10:20
357 查看
思路
获取API数据。使用Blob转化为文件对象。创建一个a标签并加入dom,并设置相关参数,其中Blob会转化为链接。触发a标签的 click 事件,然后销毁a标签。源码
Service.tsdownloadService(): any { let downloadUrl= `${RootUrl}/data/all`; return this.http.get(downloadUrl,{responseType:3}) .map(res => {
return res;
}); }123456Component.ts
downloadComponent(): void { this._listService.downloadService() .subscribe(res => {
//let res=res.json();可能要转化类型var blob = new Blob([res.json()], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); var objectUrl = URL.createObjectURL(blob); var a = document.createElement('a'); document.body.appendChild(a); a.setAttribute('style', 'display:none'); a.setAttribute('href', objectUrl); a.setAttribute('download', 'excle文件'); a.click(); document.body.removeChild(a); //释放URL地址 URL.revokeObjectURL(objectUrl);});}
{responseType:3}报错则换{'responseType':'blob'}
相关文章推荐
- 前端axios下载excel文件(二进制)的处理方法
- angular2/angular4 如何通过$http的post方法请求下载二进制的Excel文件
- 下载excel文档无法通过ajax的post请求来直接下载解决方法
- angular2/angular4 如何通过$http的post方法请求下载二进制的Excel文件
- Android webview通过http get下载文件下载两次的问题及解决方法
- Qt通过QNetworkAccessManager实现Http get方法文件下载
- 通过向服务器端发送get请求来让服务器端将其上的文件下载到客户端
- datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- 转:datagrid数据导出到excel文件给客户端下载的几种方法
- (原创)datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- 数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的三种方法
- datagrid数据导出到excel文件给客户端下载的几种方法 (转)
- datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法