java怎么判断前端取消下载了_POI 在后端处理后数据,如何让前端自动下载呢?下载前端接受的乱码,而不是文件...
现在的流程是这样的,在前端简单地发个请求,然后,后端处理数据,生成了excle文件,然后返回数据给前端。
但是前端怎么自动弹出下载框自动下载文件呢?现在接收的是乱码...
@RequestMapping(value = "/toExportData.req", method = RequestMethod.POST)
public ResponseEntity exportRequest(@RequestParam("array") String arr) {
String name = "export.xlsx";
ResponseEntity 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(body, headers, httpStatus);
System.out.println("responseEntity:" + responseEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseEntity;
}
然后,请求时成功的,并且是有东西返回的:


内容是乱码:

前端的请求代码:

理应是一个xlxs的文件才是!!!
怎么在前端接收呢??
更多推荐



所有评论(0)