download() {

const token = localStorage.getItem('token');

let headers: HttpHeaders = new HttpHeaders();

headers = headers

.set('Authorization', 'Bearer ' + token);

const url = 'http://localhost:8764/api/v1/user/downLoadZipFile';

this.http.get(url, {headers: headers, observe: 'response', responseType: 'blob'}).subscribe(response => {

console.log(response);

console.log(response.headers.keys());

this.downloadFile(response);

}, (error: HttpErrorResponse) => {

console.log(error.error);

});

}

downloadFile(data: HttpResponse<Blob>) {

const file = new Blob([data.body], {type: 'application/zip'});

const a = document.createElement('a');

a.id = 'tempId';

document.body.appendChild(a);

a.download = 'haha.zip';

a.href = URL.createObjectURL(file);

a.click();

const tempA = document.getElementById('tempId');

if (tempA) {

tempA.parentNode.removeChild(tempA);

}

}

}
var blob = new Blob([res], {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', fileName);
a.click();
URL.revokeObjectURL(objectUrl);

通用下载代码JS:

 downloadFile(filePath: any) {
this.meetingService.downloadFile(filePath, rtv => {
if (rtv) {
let _blob = new Blob([rtv]);
let _filename = filePath.substring(filePath.lastIndexOf('_') + 1);
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(_blob, _filename);
} else {
let _link = document.createElement('a');
let _url = window.URL.createObjectURL(_blob);
document.body.appendChild(_link);
_link.setAttribute('style', 'display:none');
_link.href = _url;
_link.download = _filename;
_link.click();
window.URL.revokeObjectURL(_url);
_link.remove();
}
} else {
alert('下载失败,请稍后重试!');
}
});
}

参考文章:

https://www.cnblogs.com/liugang-vip/p/7016733.html

【angular5项目积累总结】文件下载的更多相关文章

  1. 【angular5项目积累总结】遇到的一些问题以及解决办法

    1.项目中字符串特别是\r\n,替换成br之后,在页面换行无法生效? 答:绑定元素 innerHTML. <div class="panel-body" [innerHTML ...

  2. 【angular5项目积累总结】消息订阅服务

    code import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable ...

  3. 【angular5项目积累总结】侧栏菜单 navmenu

    View Code import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/co ...

  4. 【angular5项目积累总结】avatar组件

    View Code import { Component, HostListener, ElementRef } from '@angular/core'; import { Adal4Service ...

  5. 【angular5项目积累总结】breadcrumb面包屑组件

    view code <div class="fxs-breadcrumb-wrapper" aria-label="Navigation history" ...

  6. 【angular5项目积累总结】结合adal4实现http拦截器(token)

    import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRe ...

  7. 【angular5项目积累总结】文件上传

    <div class="form-group row"> <label class="col-sm-2 col-form-label"> ...

  8. 【angular5项目积累总结】自定义管道 OrderBy

    import { Injectable, Pipe } from '@angular/core'; @Pipe({ name: 'orderBy' }) @Injectable() export cl ...

  9. 【angular5项目积累总结】一些正则积累

    /^[1-9][0-9]{0,4}$/ /^[1-9][0-9]{0,4}(,[1-9][0-9]{0,4})*$/ /^([a-zA-Z0-9_\-])+\@(([a-zA-Z0-9\-])+\.) ...

随机推荐

  1. 一起学习MVC(1)初步了解MVC

    MVC 即模型视图控制器(Model View Controller)     利于团队开发.便于管理与维护.代码易读性强.未来的主流开发框架结构. 当然,缺点也显而易见,与传统开发框架相比有很大的不 ...

  2. LeetCode149:Max Points on a Line

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  3. C#实现FTP文件的上传、下载功能、新建目录以及文件的删除

    本来这篇博文应该在上周就完成的,可无奈,最近工作比较忙,没有时间写,所以推迟到了今天.可悲的是,今天也没有太多的时间,所以决定给大家贴出源码,不做详细的分析说明,如果有不懂的,可以给我留言,我们共同讨 ...

  4. .net core 与ELK(4)后台运行els可视化工具和Kibana

    which nohup .bash_profile中并source加载 如果没有就安装吧 yum provides */nohup nohup npm run start & nohup ./ ...

  5. Android TV Overscan

    本文来自网易云社区 作者:孙有军 开发的TV应用发现在部分电视上可以显示完整,而其他部分电视显示不全,周围都会遮挡了. 原因 这是因为部分老的电视有一个overscan的概览,什么叫overscan呐 ...

  6. Masnory 学习

    1:typeof <一元运算符,放在一个运算数之前,运算数可以是任何类型, 用于获取括号中的运算数的数据类型如:NSString CGFloat Int NSArray等> 2:  str ...

  7. 无法启动此程序,因为计算机中丢失QtCored4.dll。尝试重新安装该程序以解决此问题。

    在创建一个win32控制台应用程序时包含了QtCore中的头文件,并且程序编译成功(至少说明属性配置是正确的),运行此程序会出现弹出如下的一个系统错误: 这样的情况该怎么解决?提示说计算机中丢失了Qt ...

  8. cobbler 自定义私有yum源

    目的:为客户端自动添加上yum源 以下以openstack源为例 1.新建私有yum源[root@localhost ~]#cobbler repo add --name=openstack-mita ...

  9. 冒泡排序实现(Java)

    冒泡排序是一种交换排序,它的基本思路是: 两两比较相邻记录的关键字,如果反序则交换,知道没有反序的记录位置. 依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数, ...

  10. AngularJS入门之Services

    关于AngularJS中的DI 在开始说AngularJS的Service之前,我们先来简单讲讲DI(Dependency Injection,通常中文称之为“依赖注入”). DI是一种软件设计模式, ...