在Spring Boot应用程序中,链接会生成pdf文件。
Link在thymeleaf
<a th:href="@{/printings/bytesttype/compressions}" class="list-group-item list-group-item-action"><span th:text="#{compressions}">Compressions</span></a>
在控制器端
@GetMapping(value = "/printings/bytesttype/compressions")
public ResponseEntity<byte[]> getCompressionsReport() throws IOException, Exception {
return preparePdfReport(samplingFacade.getCompressionToPrint());
}
private ResponseEntity<byte[]> preparePdfReport(byte[] content) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String fileName = "report.pdf";
headers.setContentDispositionFormData(fileName, fileName);
headers.setCacheControl("no-cache, must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<>(content, headers, HttpStatus.OK);
return response;
}
实际上用户点击,一个请求被完成。
在另一个请求完成与扩展开
chrome-extension:// oemmndcbldboiebfnladdacbdfmadadm / http://localhost:8080/printings/bytesttype/compressions
有办法避免这种情况吗?
试图 Chrome sends two requests when downloading a PDF (and cancels one of them)
问题仍然存在
答案 0 :(得分:0)
如果你没有表现出与扩展(浏览器内嵌),PDF你可以尝试添加头部的HTTP响应如下:
response.setHeader("Content-Disposition", "attachment; filename=report.pdf");