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. [ACM_动态规划] hdu 1176 免费馅饼 [变形数塔问题]

    Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁 ...

  2. odoo:开源ERP/安装和初始设置

    1.1 Odoo的结构 Odoo使用Web浏览器来访问Odoo服务,因此你的Odoo服务器可以部署在较远的地方(如另外一个城市),用户的计算机上只需安装谷歌.火狐或 IE9 以上的浏览器,所以Web客 ...

  3. Day 15 内置函数 , 匿名函数.

    1. 最大值 max,最小值# #最大值 ret = max(1,2,-3)print(ret)# 结果 2ret=max([1,2,3,4])print(ret)# 结果 4 2.sum 函数用法 ...

  4. 201621123018《Java程序设计》第3周学习报告

    Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系 ...

  5. 伸展树的实现——c++

     一.介绍 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造.(01) 伸展树属于二叉 ...

  6. poj 107 DNA sorting

    关于Java的题解,也许效率低下,但是能解决不只是ACGT的序列字符串 代码如下: import java.util.*; public class Main { public static void ...

  7. 设置jade高亮

    来自:https://segmentfault.com/a/1190000002896247 打开sublime3,再打开 view > show console,把下列代码复制到console ...

  8. linux 服务器性能监控(一)

    这篇文章主要介绍一些常用的linux服务器性能监控命令,包括命令的常用参数.指标的含义以及一些交互操作. 几个问题 命令本身并不复杂,关键是你对操作系统基础知识的掌握和理解,先来看看下面几个问题: C ...

  9. HTML基本标记1.1

    HTML头部标记head 在HTML语言的头部元素中,一般包括标题,基础信息和元信息等.HTML的头部元素以<head>为开始标记,以</head>为结束标记 语法: < ...

  10. POJ 2242

    #include <iostream> #include <cmath> #include <iomanip> using namespace std; #defi ...