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. Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01

    作为微软的MVP,有一个我最喜欢的好处,就是可以与产品组(产品研发部门)有零距离接触,可以最先拿到即将发版的产品,并且和产品组沟通,对产品中出现的问题实时反馈. 看到TFS产品组吸收了自己的建议和反馈 ...

  2. 疑难杂症--SQL SERVER 2012下数据库内存异常回收

    --=================================================================== --背景: 在一台SQL SERVER 2012 SP1(1 ...

  3. NPOI datatable导出类

    using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Generic; ...

  4. netcore配置文件与发布

    这段时间好几次想写博客,但是却发现无法编辑,至今都不行 CuteEditor TinyMCE(推荐) 以上两种都不行,只好更改默认编辑器为textbox. 一直以为被封了还是怎么滴,原来是突然不支持c ...

  5. Cesium简介 [转]

    http://www.cnblogs.com/laixiangran/p/4984522.html 一.Cesium介绍 Cesium是国外一个基于JavaScript编写的使用WebGL的地图引擎. ...

  6. wpf 窗体翻页效果

    点击设置翻页,取消翻回来 1.xaml <Window x:Class="_3D翻页动画.MainWindow" xmlns="http://schemas.mic ...

  7. 【bug记录】jpa 解决org.hibernate.lazyinitializationexception could not initialize proxy - no session

    前言 最近开发项目比较忙,Spring Cloud的笔记得稍稍放放了,下午出来个bug,恶心的不行,功能很简单,也没有什么级联或复杂的映射关系,就是一直在报三个异常 Caused by: com.fa ...

  8. MySQL(作业练习)

    day59 参考:http://www.cnblogs.com/wupeiqi/p/5748496.html 现有数据库 /* Navicat Premium Data Transfer Source ...

  9. PHP开发接口,封装方法

    接口的主要功能是从服务器端获取数据,然后渲染到客户端 其主要的实现流程一般会经历这样的几个阶段服务器端----> 数据库|缓存 ----> 调用接口 ---->客户端 在接口数据传输 ...

  10. 语法糖----C#的async和await

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. public class MyClass { public MyClass() { DisplayValue ...