Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails error using HttpServletResponse to export file for download
    primarykey
    data
    text
    <p>I am using <a href="http://opencsv.sourceforge.net/" rel="nofollow">opencsv</a> in my Grails application to export attributes from my Person domain class to CSV. I am receiving the following error, however:</p> <p>Servlet.service() for servlet [default] in context with path [/myapp] threw exception [Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response] with root cause Message: getOutputStream() has already been called for this response</p> <p>From searching online, I think the answer may be to implement some responseComplete() method somewhere for the HttpServletResponse response. However, I am not sure how to do this. Any ideas? Here is my code:</p> <pre><code>def export = { def course = Course.get(params.id) if(course){ def persons = course ? Person.findAllByCourse(course) : [] response.setHeader("Content-disposition", "attachment; filename=people.csv") response.contentType = "text/csv" def out = response.outputStream out.withWriter { writer -&gt; String[] properties = new String[3] def csvWriter = new CSVWriter(writer) persons.each { person -&gt; properties[0] = person.firstName properties[1] = person.lastName properties[2] = person.email properties[3] = person.phone properties[4] = person.address1 properties[5] = person.address2 properties[6] = person.city properties[7] = person.state properties[8] = person.zip5 csvWriter.writeNext(properties) } csvWriter.flush() } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload