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. WebService 工作原理及实例教程

    一.WebService到底是什么? 先来看下标准的定义:Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统 ...

  2. Ocelot中文文档入门

    入门 Ocelot仅适用于.NET Core,目前是根据netstandard2.0构建的,如果Ocelot适合您,这个文档可能会有用. .NET Core 2.1 安装NuGet包 使用nuget安 ...

  3. 深入浅出“跨视图粒度计算”--2、INCLUDE表达式

    本文由  网易云发布. 上一篇,讲了什么是数据的粒度,以及网易有数中,哪些地方能够改变视图上的数据粒度. 现在正式开始跨视图粒度计算表达式的讲解,我们首先来看一下INCLUDE表达式 { INCLUD ...

  4. DateTimeField如何自动设置为当前时间并且能被修改 ——django日期时间字段的使用

    参考于:https://www.cnblogs.com/huchong/p/7895263.html 创建django的model时,有DateTimeField.DateField和TimeFiel ...

  5. 931. Minimum Falling Path Sum

    Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

  6. “全栈2019”Java多线程第二十一章:同步代码块产生死锁的例子

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  7. LOJ#3086. 「GXOI / GZOI2019」逼死强迫症(矩阵快速幂)

    题面 传送门 题解 先考虑全都放\(1\times 2\)的方块的方案,设防\(i\)列的方案数为\(g_i\),容易推出\(g_i=g_{i-1}+g_{i-2}\),边界条件为\(g_0=g_1= ...

  8. javascript中的数据类型和变量

    Number JavaScript不区分整数和浮点数,统一用Number表示,以下都是合法的Number类型: 123; // 整数123 0.456; // 浮点数0.456 1.2345e3; / ...

  9. php 向二维数组中追加元素

    处理之前的数据: 处理后: //$consult 为往里插之前的数组 //把$arr的元素追加到$consult的最前面 $arr = []; $arr[0]['workplaceId'] = '0' ...

  10. IntelliJ IDEA优秀插件(编程通用)

    一.IntelliJ IDEA开发 最近大部分开发IDE工具都切换到了,所以也花了点心思去找了相关的插件.这里整理的适合各种语言开发的通用插件,也排除掉IntelliJ IDEA自带的常用插件了(有些 ...