Ajax請求匯出(下載)檔案
阿新 • • 發佈:2019-01-05
最近做檔案匯出,由於登入認證方式改為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; }