Search code examples
grailsexportgrails-plugin

Grails export plugin don't download any file


I want to use grails export plugin to get my Domain classes exportables into xls and csv files.

In my main layout named front.gsp, I did that :

<!DOCTYPE html>
<html lang="en">
    <head>
        ...
        <g:layoutHead />
    </head>
    <body>
        <sec:ifLoggedIn>
        <r:require module="export"/>
        <export:formats formats="['csv', 'excel', 'ods', 'pdf', 'rtf', 'xml']" action="exportTest" />
        ...
        <g:layoutBody />
        <r:layoutResources/>
        <script type="text/javascript" src="${resource(dir: 'js', file: 'jquery.min.js')}"></script>
        <script type="text/javascript" src="${resource(dir: 'js', file: 'bootstrap.min.js')}"></script>
        <script type="text/javascript" src="${resource(dir: 'js', file: 'application.js')}"></script>
    </body>
</html>

Into my DomainClassController.groovy I did that :

def exportTest() {
        if(!params.max) params.max = 10

        if(params?.format && params.format != "html"){
            response.contentType = grailsApplication.config.grails.mime.types[params.format] response.setHeader("Content-disposition", "attachment; filename=contacts.${params.extension}")

            exportService.export(
                params.format,
                response.outputStream,
                ContactDTO.list(params),
                [:],
                [:])
            [contactDTOInstanceList: ContactDTO.list( params )]
        }
    }

I also create a view named exportTest.gsp into my view folder of my controller, just empty file to temporarely solve 404 issue.

I did not have any error messages but when I'm clicking on any export button, I'm redirecting to my page exportTest.gsp and no file is downloaded

How can I get a downloaded file using grails export plugin ? I follow the user guide, don't have any errors (solved) but no file is created ?

Thanks for reading !

Snite

EDIT : I just see that, when clicking on export link, the generated url is :

http://localhost:8080/site/controller/action?format=excel&extension=xls

But strangely, when making a "println params" into my controller, output is :

[extension:xls, action:exportTest, format:null, controller:contactDTO, max:10]

params.format is null BUT set into url ?!!!

Change url like that (and adapting controller code) :

http://localhost:8080/site/controller/action?formatD=excel&extension=xls

Give me the following error :

Firefox ne peut trouver le fichier à l'adresse http://localhost:8080/site/controller/action?formatd=excel&extension=xls.

Solution

  • My guess is the params.format is getting confused with Grails content negotiation format in parameter. The export tag seems to generate that format, unless that gets fixed you can create a link to your exportTest and pass the format you want under lets say exportFormat variable name. On your controller use exportFormat instead of format.

    Also make sure that ContactDTO.list(params) is returning some values. Another point is make sure you have defined the content types in you config as it says in the export plugin documentation

    So you might need to create a link like this:

    <a class="csv" href="/testData/loader/exportTest?exportFormat=csv&extension=csv"> 
      CSV 
    </a>
    

    In your controller :

    def exportTest() {
            if(!params.max) params.max = 10
    
            if(params?.exportFormat && params.exportFormat != "html"){
                response.contentType = grailsApplication.config.grails.mime.types[params.exportFormat] 
                response.setHeader("Content-disposition", "attachment; filename=contacts.${params.extension}")
    
                exportService.export(
                    params.exportFormat,
                    response.outputStream,
                    ContactDTO.list(),
                    [:],
                    [:])
                [ContactDTO: ContactDTO.list( params )]
            }
        }