我是靠谱客的博主 勤恳草丛,最近开发中收集的这篇文章主要介绍Ajax请求导出(下载)文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

最近做文件导出,由于登录认证方式改为oauth方式,请求后台导出接口需要设置请求头。所以传统的通过form提交的方式并不能满足需求,故而通过原生ajax的方式来实现。

1.实现前端html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试export</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
var xhh = new XMLHttpRequest();
xhh.open("post", "http://192.168.20.5:1105/pay/w/reveDisbDetails/exportByMemb?accId=4459371232172851870");
//xhh.open("post", "http://192.168.20.5:1105/pay/w/reveDisbDetails/exportByMemb");
xhh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhh.setRequestHeader("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ3YW5naCIsIm9wZW5JZCI6ImM0ZDhlZjVlOWFlODQzODE4MWI2YmI4ZWM3ZjRiZWE2Iiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImV4cCI6MTUyMDkzMDU0OSwidXNlcklkIjo0NDU5MzcxMjMyMTcyODUxODcwLCJhdXRob3JpdGllcyI6WyJtZW1iOmFjYyIsIm1lbWI6bXNnIiwic2FsZXI6ZXZhbCIsImJ1eWVyOnRvcmQiLCJidXllcjpnb2Q6ZXhtIiwibWVtYjphZGRyIiwibWVtYjppbmZvIiwiUk9MRV91c2VyIiwiYnV5ZXI6Y29udCIsIm1lbWI6ZGVyOmxvZyIsInNhbGVyOnRvcmQiLCJidXllcjp0aWM6ZXhtIiwiYnV5ZXI6ZXZhbCIsInNhbGVyOnRpYzpleG0iLCJtZW1iOmludm8iLCJtZW1iOnBheXBhc3MiLCJzYWxlcjpvcmQiLCJtZW1iOnBhc3MiLCJtZW1iOmluZm86dXBkIiwibWVtYjp3aXRoZHJhdyIsIm1lbWI6ZGlydCIsInNhbGVyOmNvbnQiLCJzYWxlcjpnb2Q6ZXhtIiwibWVtYjpkZXQiLCJtZW1iOmRlciIsImJ1eWVyOm9yZCIsImJ1eWVyOnd0YiIsIm1lbWI6ZGVyOnVwZCIsIlJPTEVfYWNjb3VudCIsIm1lbWI6aW50ZWciLCJzYWxlcjpoYW5kb3V0Il0sImp0aSI6IjRhMjIzYzk3LWMxMTAtNDVmOC04NjM3LTBjMDQ4Nzk1MzFhZiIsImNsaWVudF9pZCI6Ind0cC1zZWN1cml0eSJ9.QjhTyIbP_pGHr9n83neHzzngaxJnFxS1wg_6EHKioDq4eYzOAewyQH5tHehXU4O4OtF9k6uDtue9VQAHIfSfZFY6mSOna8MkJ8A2Gp4AfVjSa36t5UCdljY-J72wunGKnJMTE83gJeEyurRATjTaQzxtSQY7qebmhGIdLjdI6Ro");
xhh.responseType = 'blob';
xhh.onreadystatechange = function () {
if (xhh.readyState === 4 && xhh.status === 200) {
alert("bb");
var name = xhh.getResponseHeader("ajax-filename");
var mimeType = xhh.getResponseHeader("ajax-mimeType");
var blob = new Blob([xhh.response], {type: mimeType});
var csvUrl = URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = csvUrl;
link.download = name;
link.click();
}
};
xhh.send();
});
});
</script>
</head>
<body>
<button id="btn">测试哈哈</button>
</body>
</html>

2.后台response加入头信息

public ExportExcel writeWithAjaxHeader(HttpServletResponse response, String fileName) throws IOException{
//response.reset();
ServletContext context = RequestContextUtils.getCurrentRequest().getServletContext();
String mimeType = context.getMimeType(fileName);
String fName = ConverterUtil.toDownloadName(URLEncoder.encode(fileName, "utf8"));
response.addHeader("Access-Control-Expose-Headers", "ajax-mimeType, ajax-filename");
response.setHeader("Content-Disposition", "attachment; filename="+fName);
response.setHeader("ajax-mimeType", mimeType);
response.setHeader("ajax-filename", fName);
response.setContentType("application/octet-stream; charset=utf-8");
write(response.getOutputStream());
return this;
}

最后

以上就是勤恳草丛为你收集整理的Ajax请求导出(下载)文件的全部内容,希望文章能够帮你解决Ajax请求导出(下载)文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部