惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
D
Docker
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Recent Announcements
Recent Announcements
GbyAI
GbyAI
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
L
LangChain Blog
A
About on SuperTechFans
M
MIT News - Artificial intelligence
B
Blog

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
ResponseEntity下载文件
祈雨的笔记 · 2018-12-17 · via 祈雨的笔记

前端依赖

FileSaver.js

前端angular

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import FileSaver from '../xxx/FileSaver.js';

const params = {
};
return this.$http({
method: 'GET',
url: '/download.json',

responseType: 'arraybuffer',
params: params
}).then(function (response) {
let body = response.data;
let contentDisposition = response.headers('Content-Disposition');

let fileName;
for (let key of contentDisposition.split(';')){
let name = key.split('=')[0].trim();
if (name === 'filename'){
fileName = key.split('=')[1].trim();

fileName = decodeURIComponent(escape(fileName));

fileName = fileName.replace(/"/g, '');
}
}
if (body) {
let file = new Blob([body], {
type: "application/octet-stream"
});
FileSaver.saveAs(file, fileName);
} else {

}
}).catch(error => {
this.$log.log(error);
});

后端

1
2
3
4
5
6
7
8
9
10
@GetMapping("/download.json")
public ResponseEntity<byte[]> download() {
byte[] bytes;
HttpHeaders headers = new HttpHeaders();
String filename = new String(new String("file.xlsx").getBytes("utf-8"), "iso-8859-1");
headers.setContentDispositionFormData("attachment", filename);
headers.setContentLength(bytes.length);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
}