【问题标题】:Chrome generate two requestChrome 生成两个请求
【发布时间】:2019-06-26 23:41:54
【问题描述】:

在 Spring Boot 应用程序中,一个链接会生成一个 pdf 文件。

百里香叶中的链接

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

问题依然存在

【问题讨论】:

    标签: spring google-chrome chromium


    【解决方案1】:

    如果您不必显示带有扩展名的 PDF(浏览器内联),您可以尝试在 HTTP 响应中添加标头,如下所示:

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

    【讨论】:

    • 同样的问题,无法解决
    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    相关资源
    最近更新 更多