0

I have an input field where I enter a value, the millis parameter, that is used as parameter to generate a Jasperreport. I´m using Ajax to call the controller that generates the report. The problem is that, although the report seems to be successfully generated, the download dialog is never shown.

Here´s part of my code:

/*THE BUTTON USED FOR MY GSP VIEW*/
<button id="imprimir" type="button">IMPRIMIR LOTES LIQUIDADOS</button>


/*JAVASCRIPT CODE TO CALL THE CONTROLLER*/
$("#imprimir").bind("click",imprimir);

function imprimir(){
        $.ajax({
            url: "/Liquidaciones/liquidacionDeComplejo/crearReporteGrupal2",
            dataType: 'json',
            data: {
                millis:$("#millis").val()
            },
            success: function(data) {
            },
            error: function(request, status, error) {
            }
        });
    }

/*GRAILS CONTROLLER THAT GENERATES DE REPORT*/
def crearReporteGrupal2 = {
        Map reportParams = [:]
        def millis = params.millis.toBigDecimal()
        def realPath = servletContext.getRealPath("/reports/images/")
        reportParams.put("millis",millis)
        reportParams.put("realPath",realPath+"/")
        reportParams.put("SUBREPORT_DIR","${servletContext.getRealPath('/reports')}/")

        def reportDef = new JasperReportDef(name:'liquidacion_grupal_complejo.jasper',fileFormat:JasperExportFormat.PDF_FORMAT,parameters: reportParams)
        byte[] bytes
        bytes = jasperService.generateReport(reportDef).toByteArray()
        response.addHeader("Content-Disposition", 'attachment; filename="liquidacion_grupal.pdf"')
        response.contentType = 'application/pdf'
        response.outputStream << bytes
        response.outputStream.flush()
        //I read somewhere that is necessary to return 'something' to initiate the download
        //but it didn't help
        //return null
        //render [:] as JSON
    }

Here the result from the server: server_response

But when I copy this URL:/Liquidaciones/liquidacionDeComplejo/crearReporteGrupal2?millis=1420757752558 from the Web Console and paste it in a new tab the download dialog appears. download_dialog

Please help me!

Thanks in advance.

Manuel Calles
  • 185
  • 1
  • 1
  • 19

1 Answers1

1

You cannot generate any reports/files from ajax. I will suggest you to make a form in you view and submit it with required parameters to the same controller and action. It will return the dialog box to save your report as mentioned by you.

Code:

function imprimir(){
            $('<form>', {
                "id": "imprimir",
                "html": '<input type="text" id="millis" name="millis" value="' + $("#millis").val() + '" />',
                "action": '/Liquidaciones/liquidacionDeComplejo/crearReporteGrupal2'
            }).appendTo(document.body).submit();
        }

Hope this helps. Thanks

Biswas
  • 598
  • 2
  • 16
  • 34
  • Thank you, that worked! But I still have a doubt, why can't be generated reports/files from Ajax? – Manuel Calles Jan 14 '15 at 20:47
  • 1
    You can generate files using ajax but it has some pain while downloading file through ajax. http://stackoverflow.com/a/14683228/744553 – sAaNu Jan 15 '15 at 08:54