Chrome产生两个请求

时间:2019-02-02 17:41:26

标签: spring google-chrome chromium

在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)

问题仍然存在

1 个答案:

答案 0 :(得分:0)

如果你没有表现出与扩展(浏览器内嵌),PDF你可以尝试添加头部的HTTP响应如下:

response.setHeader("Content-Disposition", "attachment; filename=report.pdf");