1
스프링 부트 웹 응용 프로그램이 있습니다.이 응용 프로그램은 확장명이 .xlsx
인 Microsoft Excel 파일을 생성합니다.ajax로 다운로드 한 Excel 파일이 손상되었습니다.
localhost:8080/report/stats
을 호출하는 브라우저에서 파일을 다운로드하려고하면 올바른 파일이 반환되고 항상 성공적으로 열 수 있습니다.
하지만 웹 페이지에서 버튼을 클릭하여 파일을 다운로드 할 때 잘못된 파일이 표시되고 열 수 없습니다.
I가 JS에서 다음과 같은 부분 :
$.ajax({
url: 'report/stats',
type: "GET",
success: function (data) {
var link = document.createElement('a');
link.download = 'report.xlsx';
link.href = 'data:,' + data;
link.click();
}
});
컨트롤러 :
@GetMapping("stats")
public ResponseEntity downloadStatsReport() throws IOException {
return fileResponse(excelReportService.create(new StatFilter()));
}
private ResponseEntity fileResponse(File report) throws IOException {
InputStreamResource resource = new InputStreamResource(new FileInputStream(report));
return ResponseEntity.ok()
.contentLength(report.length())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + report.getName())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
JS에서뿐만 아니라 브라우저에서 작품을 작품 다운로드하는 이유는 무엇입니까?
파일 열기 오류 :
YES 클릭되었습니다
NO을 클릭 할 필요가있다 :
이 대답으로 문제가 해결되기를 바랍니다. https://stackoverflow.com/questions/36134095/file-download-through-ajax –
@DharmaSaputra, 답장을 보내 주셔서 감사 드리며 해결책을 시도했지만 실제로 작동하지만 다음 해결책을 사용하면 요청이 완료 또는 아니요 ... –