Grails Export Plugin Does Not Download Files

I want to use the Grails export plugin to export my classes to xls and csv classes.

In my main layout called front.gsp, I did this:

<!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> 

In my DomainClassController.groovy, I did this:

 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 called exportTest.gsp in my view folder of my controller, just an empty file to temporarily fix 404 problem.

I had no error messages, but when I click the export button, I am redirected to the exportTest.gsp page and the file does not load

How to get downloaded file using Grails graphics adapter? I follow the user manual, there are no errors (allowed), but the file is not created?

Thank you for reading!

Snite

EDIT: I just see that when the export link is clicked, the generated URL is:

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

But strangely enough, when creating the "println parameters" in my controller, the output is:

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

params.format has a null BUT value set to a URL? !!!

Change the url (and adapt the 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. 
+4
source share
2 answers

My guess is: params.format gets confused with the Grails content negotiation format in the parameter. It seems that this export tag will generate this format, unless that is fixed, you can create a link to your exportTest and pass in the format you want, and let's say the name of the exportFormat variable. On your controller, use exportFormat instead of format.

Also make sure ContactDTO.list(params) returns some values. One more thing - make sure that you define the content types in your config, as the documentation export plugin says

Thus, you may need to create a link as follows:

 <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 )] } } 
+6
source

I made some going this way

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

The explanation . Since params.format is null , I did a system print for the extension. And I notice that the extension matches the format for the whole extension except xls. Therefore, if xls I, the format must be "excel" for exporting excel. That's why. In my case, it works fine for csv, excel, pdf and xml. Not tested with ODD, etc.

0
source

All Articles