PDF Blob-Window.open不显示内容

时间:2019-10-24 11:41:11

标签: javascript vue.js vuejs2

我正在尝试在前端显示来自后端的pdf文件,但是新窗口仅显示PDF控件。 有人可以评估我的代码并指出我在做什么错吗?

后端代码:

@RequestMapping(value = "/capaRdv/{idRdv}", method = RequestMethod.POST)
    public void relatorioCapaRdv(@PathVariable Integer idRdv, HttpServletResponse response) throws JRException, SQLException, IOException {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("idRdv", idRdv);
        InputStream jasperStream = this.getClass().getResourceAsStream("/reports/capaRdv.jasper");
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline; filename=capaRdv" + idRdv + ".pdf");
        final OutputStream outStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
    }

前端代码:

emiteRelatorio(idRdv) {
  const url = `${baseApiUrl}/relatorios/capaRdv/${idRdv}`;
  axios
    .post(url, { responseType: "blob" })
    .then(res => {
      const file = new Blob([res.data], { type: "application/pdf" });
      const fileURL = URL.createObjectURL(file);
      window.open(fileURL);
    })
    .catch(error => {
      Swal.fire({
        text: error.response.data.error,
        type: "error",
        confirmButtonClass: "md-button md-danger btn-fill",
        buttonsStyling: false
      });
    });
}

https://imgur.com/KbfSRJ3

https://imgur.com/jP2ZJx5

1 个答案:

答案 0 :(得分:0)

通过将POST方法更改为GET来解决:

method = RequestMethod.GET

axios.get

相关问题