通过ajax在浏览器选项卡中显示pdf

showing pdf in browser tab through ajax

本文关键字:显示 pdf 选项 ajax 浏览器 通过      更新时间:2023-09-26

我曾使用jquery.fileDownload和Spring通过Ajax下载.pdf文件。

应用程序运行良好,pdf文件下载成功。问题是,我不想下载pdf,而是想在新的浏览器选项卡中打开。

有人能告诉我这个的一些解决方案吗

服务器端

我在服务器端使用Spring

@RequestMapping(value = "exportPDF", method = RequestMethod.POST, produces = APP_JSON)
@ResponseBody
public void getPDF(final HttpServletResponse response, @RequestParam(value = "empId", required = true) final String empId) throws IOException, Exception
{
    final byte[] pdf= ExportPDFUtil.getFileBytes(empId); // get the file bytes
    final OutputStream output = getOutputStream(response);
    response.setHeader("Content-Disposition", "attachment; filename=documents_" + new DateTime() + ".pdf");
    response.setContentType(CONTENT_TYPE);
    response.setContentLength(pdf.length);
    write(output, pdf);
}

客户端

在客户端,我使用AngularJS

$downloadXLS = function(id)
{
    $.fileDownload('/user/exportPDF', 
    {
        httpMethod : "POST",
        data : {
            empId : id
        }
    }).done(function(e, response)
    {
     // success
    }).fail(function(e, response)
    {
     // failure
    });
}

查看Mozilla的PDF.js。大多数应用程序都使用它在浏览器中显示PDF。

尝试使用这个:

response.setHeader("Content-Disposition", "inline; 
                  filename=documents_" + new DateTime() + ".pdf");

而不是:

response.setHeader("Content-Disposition", "attachment;
                  filename=documents_" + new DateTime() + ".pdf");