各位大佬好,我有一个同事后端写了这么一端excel文件输出代码,请问我前端js要怎么接收?
Workbook workbook = exportExcel(lsMap, ExcelType.HSSF);
request.setCharacterEncoding("utf-8");
respones.setCharacterEncoding("utf-8");
respones.setContentType("application/x-download");
String filedisplay = "product.xls";
filedisplay = URLEncoder.encode(filedisplay, "utf-8");
respones.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
OutputStream out = respones.getOutputStream();
workbook.write(out);
out.close();
方法1: 无需js,直接用a标签去接你的输出流
<a href="<你的返回流的Action路径>" >下载</a>
方法2:使用js,前提是你使用的是现代浏览器,支持html5特性(主要是新的XMLHTTPRequest对象,Blob)
<script type="text/javascript"> function download(filename) { var oReq = new XMLHttpRequest(); oReq.open("GET", "<你的返回流的Action路径>", true); oReq.responseType = "blob"; oReq.onload = function (oEvent) { var content = oReq.response; var elink = document.createElement('a'); elink.download = filename; elink.style.display = 'none'; var blob = new Blob([content]); elink.href = URL.createObjectURL(blob); document.body.appendChild(elink); elink.click(); document.body.removeChild(elink); }; oReq.send(); } </script>