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: Tugis Grokazahn
Country: Turkey
Language: English (Spanish)
Genre: Literature
Published (Last): 15 October 2011
Pages: 349
PDF File Size: 1.47 Mb
ePub File Size: 13.98 Mb
ISBN: 948-4-71769-259-6
Downloads: 91886
Price: Free* [*Free Regsitration Required]
Uploader: Masar

B I am using the mime type and encoding defined in application. A CSV file is easy to import in Excel.

A I will recommend to put the logic fetching the lines in a Service. I also typed the methods as void responsr.outputstream I am still too Java-fussy? If you like my posts, like my Facebook page please: The full source code for this example can be viewed here or can be downloaded here.

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

If that’s the case, copy from stream to stream. You may need to add additional graiils signatures so that they can be statically compiled but the concept is the same. September 5, Grails Goodness: Grails will try to find the content type based on the extension of the filename.

Does anyone have any suggestions for me? You can fix this by adding CompileStatic to your method, but that is not always feasible. This will also set a response header with the name Content-Disposition with a the filename as value.


The output of the finished application will look like this: Lastly, cloze the ZIP: Then we use the render method and the file and contenType attributes to render the image in a browser: Just instantiate ZipOutputStream passing response. C If you want the file to download you need to setup the Content-disposition header.

You just provide the contents of the file by providing it’s binary: Stack Overflow works best with JavaScript enabled. GrailsConfigurationAware import static org. What happens is that whenever I access it, Firefox fires up Totem plugin which streams the file from the controller. Daniel 1, 1 3 CSV is probably the best format to export your data from a Grails App. Learn more about retrieving config values.

ServletOutputStream to make this work. I wrote a post; in Spanish though. Will give this a shot. Post Your Answer Discard By clicking “Post Your Answer”, you acknowledge that you have read our updated terms of serviceprivacy policy and cookie policyand that your continued use of the website is subject to these policies.

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. We can use the fileName attribute to graips a filename for the binary content. Proper way to stream file from controller. Notify me of new posts by email.

Grails User (Old Archive) – Proper way to stream file from controller

Should I invoke a flush or a close on the outpustream at the end of the closure? Render the download page The controller and view code to display response.outputatream download page should be simple: And also the second file: Your email address will not be published. Hi all, I have a controller which would stream file as response.

Related Posts  RFC 2409 PDF

In a controller I have a method that appends a string to the response. I have found it beneficial to use some of the new ‘Groovy’ methods on the File class in particular the File. I’m glad it was useful to you, and thanks for the additional info for anybody else who needs this!

Proper way to stream file from controller

The code now appears as response. Notify me of follow-up comments by email. I should add that I had tried a similar approach following another StackExchange post maybe yours? On Wed, Jul 30, at 2: I will then get an IllegalStateException: In reply to this post by Lauk Daniel. On click, a download confirmation will be shown instead of the browser trying to render the content: In the following controller we find an image in our application using grailsResourceLocator.