【angular5项目积累总结】文件下载
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项目积累总结】文件下载的更多相关文章
- 【angular5项目积累总结】遇到的一些问题以及解决办法
1.项目中字符串特别是\r\n,替换成br之后,在页面换行无法生效? 答:绑定元素 innerHTML. <div class="panel-body" [innerHTML ...
- 【angular5项目积累总结】消息订阅服务
code import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable ...
- 【angular5项目积累总结】侧栏菜单 navmenu
View Code import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/co ...
- 【angular5项目积累总结】avatar组件
View Code import { Component, HostListener, ElementRef } from '@angular/core'; import { Adal4Service ...
- 【angular5项目积累总结】breadcrumb面包屑组件
view code <div class="fxs-breadcrumb-wrapper" aria-label="Navigation history" ...
- 【angular5项目积累总结】结合adal4实现http拦截器(token)
import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRe ...
- 【angular5项目积累总结】文件上传
<div class="form-group row"> <label class="col-sm-2 col-form-label"> ...
- 【angular5项目积累总结】自定义管道 OrderBy
import { Injectable, Pipe } from '@angular/core'; @Pipe({ name: 'orderBy' }) @Injectable() export cl ...
- 【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\-])+\.) ...
随机推荐
- 【LongIntervalRetries】让我们来实现支付宝的异步回调方案
功能说明 LongIntervalRetries是基于Quartz.Net的一个长时间间隔重试的类库,其主要解决何时执行以及执行结果反馈的问题. 产生的原因 简单的说,我们提供了一系列的API供第三方 ...
- 嵌入的资源 和 Resource
我们将资源文件添加至.net C#工程时,文件的生成操作有多种可选方式.通常用的多的是两种:[嵌入的资源]和[Resource],如果从需要从代码中使用这些资源文件,不同生成操作则对应不同的引用方式: ...
- python salt 实现windows账户自动化
import random import string import json import logging import time import os import sys def usage(): ...
- sql语句_统计总成绩最高的前2名
有一个数据表,id user_id score 三个字段,计算总成绩最高的前两名 SELECT * FROM (SELECT user_name,SUM(score) AS score FROM us ...
- POJ 2828Buy Tickets(线段树的单点维护)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 20462 Accepted: 10096 Des ...
- 变不可能为可能 - .NET Windows Form 改变窗体类名(Class Name)有多难?续篇
发布<.NET Windows Form 改变窗体类名(Class Name)有多难?>转眼大半年过去了,要不是在前几天有园友对这篇文章进行评论,基本上已经很少关注它了,毕竟那只是一个解惑 ...
- Java抽象工厂模式
Java抽象工厂模式 基本定义 抽象工厂模式是所有形态的工厂模式中最为抽象和最其一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象 ...
- 【hdu4609】 3-idiots FFT
题外话:好久没写blog了啊-- 题目传送门 题目大意:给你m条长度为ai的线段,求在其中任选三条出来,能构成三角形的概率.即求在这n条线段中找出三条线段所能拼出的三角形数量除以$\binom{m}{ ...
- linux下mysql主从复制搭建
目标:搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,实现主从复制 环境: 主数据库: 192.168.1.1 从数据库: 192.168.1.2 mysql安装可参考:https:// ...
- 51单片机SRF寄存器
1.21个寄存器介绍 51系列单片机内部主要有四大功能模块,分别是I/O口模块.中断模块.定时器模块和串口通信模块(串行I/O口),如其结构和功能如下图: 图1 51单片机结构和功能图 ...