• No Comments

Grails Programmer: How to output CSV from a Grails 3 Controller. 20 / 1 Reply def outs = Stream. = (). response. Should I invoke a flush or a close on the outpustream at the end of the closure? () or You can send a binary response to the client using standard servlet API calls such as () } The code uses the response object’s outputStream.

Author: Mell Neshakar
Country: Indonesia
Language: English (Spanish)
Genre: Art
Published (Last): 7 January 2018
Pages: 50
PDF File Size: 13.66 Mb
ePub File Size: 18.17 Mb
ISBN: 958-4-54708-942-2
Downloads: 12772
Price: Free* [*Free Regsitration Required]
Uploader: JoJozuru

This will also set a response header with the name Content-Disposition with a the filename as value. Post as a guest Name.

Based from this, it is easy to modify the sample to put different types of files inside the output ZIP. We have fixed this problem in our applications by adding a static utility method: A weekly curated email newsletter about the Groovy ecosystem.

Proper way to stream file from controller

In the following controller we find an image in our application using grailsResourceLocator. We have fixed this problem in our applications by adding a static utility method:. Sign up using Facebook. Are you facing any problems with that?

Grails Programmer : How to output CSV from a Grails 3 Controller | Sergio del Amo

A I will recommend to put the logic fetching the lines in a Service. Since Grails 2 we can render binary output with the render method and the file attribute. On click, a download confirmation will be shown instead of the browser trying to render the content: By using our site, resppnse.outputstream acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. If the byte arrays in your examples are large, or if the ‘download’ action is called frequently, you could run into problems.


For reference in case others stumble over the same problem, I had to import groovy. Render Binary Grzils with the File Attribute. Most browser will then automatically download the binary content, so it can be saved on disk.

This is nice and clean and works just fine. If that’s the case, copy from stream to stream. If the answer is yes, subscribe to Groovy Calamari. Then we use the render method and the file and contenType attributes to render the image in a browser:.

Grails will try to find the content type based on the extension of the filename.

Grails Programmer : How to output CSV from a Grails 3 Controller

Currently, here’s how I do it: Creating a file inside the zip is accomplished by instantiating a ZipEntry and put it inside the ZIP file. Tell the browser that your output is binary and the filename for download: I should add that I had tried a similar approach following another StackExchange post maybe yours? If you like my posts, like my Facebook page please: The output of the finished application will look like this: I believe I’ve read somewhere that I might be able to cure this problem by replacing Tomcat7 with Tomcat8; however right now I’m running Ubuntu Does anyone have any suggestions for me?


B I am using the mime type and encoding defined in application. Your email address will not be published. Render the download page The controller and view code to display the download page should be simple: Will give this a shot.

A CSV file is easy to import in Excel. On Wed, Jul 30, at 9: In a controller I have a method that appends a string to the response. Search everywhere only in this topic.

If we run the app and call the controller we will download a CSV file as this: The full source code for this example can be viewed here or can be downloaded here.