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.
|Published (Last):||23 February 2018|
|PDF File Size:||11.58 Mb|
|ePub File Size:||11.52 Mb|
|Price:||Free* [*Free Regsitration Required]|
You just provide the contents of the file by providing it’s binary: The full source code for this example can be viewed here or can be downloaded here.
Post as a guest Name. Most browser will then automatically download the binary content, so it can be saved on disk. If we run the app and call the controller we will download a CSV file as this: The BootStrap respojse.outputstream closure runs when the app starts.
Email Required, but never shown. On Wed, Jul 30, at 9: I was tired of my clients asking me how to import a CSV in Excel.
Render Binary Output with the File Attribute. On Wed, Jul 30, at 1: This is nice and clean and works just fine. Responae.outputstream 5, Grails Goodness: If I recall correctly, the left shift operator here uses some annotation or something clearly I don’t remember details! ServletOutputStream to make this work.
Grails Goodness: Render Binary Output with the File Attribute – Messages from mrhaki
Sign up using Email and Password. GrailsConfigurationAware import static org. Use TAB for completion: You may need to add additional method signatures so that they can be statically compiled but the concept is the grrails.
Grails Programmer : How to output CSV from a Grails 3 Controller
I’m glad it was useful to you, and thanks for the additional info for anybody else who needs this! Just instantiate ZipOutputStream passing response. Thanks If you are using Grails 2, just change the setConfiguration part to this: Proper way to stream file from controller. You can fix this by adding CompileStatic to your method, but that is not always feasible. Other than that, so far things are ok. If the byte arrays in your examples are large, or if the ‘download’ action is called frequently, you could run into problems.
Creating a file inside the zip is accomplished by instantiating a ZipEntry and put it inside the ZIP file. We can use the fileName attribute to set a filename for the binary content.
Proper way to stream file from controller
I have a Grails application that I have recently upgraded to 3. We have fixed this problem in our applications by adding a static utility method:. Handler dispatch failed; nested exception is java.