现在的流程是这样的,在前端简单地发个请求,然后,后端处理数据,生成了excle文件,然后返回数据给前端。
但是前端怎么自动弹出下载框自动下载文件呢?现在接收的是乱码...
@RequestMapping(value = "/toExportData.req", method = RequestMethod.POST)
public ResponseEntity<byte[]> exportRequest(@RequestParam("array") String arr) {
String name = "export.xlsx";
ResponseEntity<byte[]> responseEntity = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] body = null;
InputStream is = null;
try {
createExcelFile().write(os);
body = os.toByteArray();
System.out.println("读取 输入流.....");
HttpHeaders headers = new HttpHeaders();
HttpStatus httpStatus = HttpStatus.OK;
System.out.println("设置headers。。。。");
headers.add("Content-Disposition", "attachment;filename=export.xlsx");
headers.setContentType(MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
System.out.println("headers is: " + headers);
responseEntity = new ResponseEntity<byte[]>(body, headers, httpStatus);
System.out.println("responseEntity:" + responseEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseEntity;
}
然后,请求时成功的,并且是有东西返回的:
内容是乱码:
前端的请求代码:
理应是一个xlxs的文件才是!!!
怎么在前端接收呢??
大致看了下,你這是要下載對吧,直接把你產生的excel 資料 用流用前段寫不就是下載了,請求頭你不是都設定好了