Grails export plugin don't download any file
Asked Answered
C

3

4

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.
Czernowitz answered 13/11, 2013 at 18:16 Comment(0)
C
6

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 )]
        }
    }
Chicky answered 14/11, 2013 at 6:9 Comment(2)
It was the point, I tested yesterday renaming params.format to params.formatt and it solve my problem. Thanks for explanations and your time !Czernowitz
I've forked (github.com/lrkwz/grails-export) and issued a pull request in order to fix this (github.com/nwwells/grails-export/pull/7)Ethical
C
0

I did some by passing through this way

params.format=params.extension<br/>
        if(params.format=='xls') {
            params.format='excel'
        }

Explanation: As the params.format is null, I did a system print for extension, And I remark that the extension is the same as format for all extension except xls. So if xls I the format must be 'excel' for excel exporting. This is why. In my cas it works fine for csv, excel, pdf and xml. Not tested with ODD etc..

Collaborationist answered 24/6, 2015 at 13:37 Comment(0)
D
0

I am using export plugin version 2.0.0 with Grails 3. As per the internal implementation in Plugin's ExportTagLib.groovy the argument format has been renamed to avoid conflict with grails 3

ExportTagLib.groovy

Map params = [f: format, extension: extension] + parameters

Using params.f will work fine for now. Sample code.

        if (params.f && params.f != "html") {
        response.contentType = Holders.config.grails.mime.types[params.f]
        response.setHeader("Content-disposition", "attachment; filename=${message(code: 'dpInvoice.exportBillFacilitator.filename', default: 'exportBillFacilitator')}.${params.extension}")
        exportService.export(params.f, response.outputStream, results, fields, labels, formatters, parameters)
    }
Dior answered 29/6, 2018 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.