【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\-])+\.) ...
随机推荐
- LeetCode147:Insertion Sort List
题目: Sort a linked list using insertion sort. 解题思路: 按题目要求,直接进行插入排序 实现代码: #include <iostream> us ...
- Python3------装饰器详解
装饰器 定义:本质是函数.(装饰其他函数)就是为其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 理解装饰器前提条件: 1.函数即"变量&qu ...
- 深入理解Aspnet Core之Identity(3)
主题 账户管理一个比较常见的功能就是密码强度策略,Identity已经内置了一个通用的可配置的策略,我们一般情况下可以直接拿来用即可.本篇我会介绍一些Identity内置的密码策略类:Password ...
- c# 将一个长耗时任务改成task
public Task<ObservableCollection<WaterFallDataSource2>> InitalrenderSourceandBrush2(int ...
- 基于EasyUi的datagrid合并单元格JS写法
$('#dg').datagrid({ width: 'auto', height: 'auto', scrollbarSize: , queryParams: {}, url: 'kkkk', co ...
- 极大提高Web开发效率的8个工具和建议(含教程)
面对复杂的 Web 应用的开发,良好的流程和工具支持是必不可少的,它们可以让日常的开发工作更加顺畅.更加高效.本文介绍了6个Web开发利器以及相关的教程,帮助你在开发.调试.集成和发布过程极大地提高效 ...
- Django(app的概念、ORM介绍及编码错误问题)
day61 Django中的APP: 什么是APP?以及为什么要用APP? project --> 项目 (老男孩教育大学校) ...
- orm 练习题
一: 多表练习查询 1. 自行创建测试数据: 2. 查询学生总人数: 3. 查询“生物”课程和“物理”课程成绩都及格的学生id和姓名: 4. 查询每个年级的班级数,取出班级数最多的前三个年级: 5. ...
- csc命令
CSC.exe把Visual C#程序代码编译成IL文件时,有着很多参数和开关选项.正确的了解和运用这些参数和开关有时会解 决一些看似很棘手的问题.下面就通过一张表来大致说明一下这些参数和开关的具体作 ...
- SAS->关联分析实践
SAS系统被誉为国际上的标准软件系统,本文将详细介绍如何在SAS/EM模块中进行关联规则数据挖掘,使用的软件版本是SAS 9.1.3下的Enterprise Miner 4.3: 从SAS顶端的[解决 ...