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):||15 October 2011|
|PDF File Size:||1.47 Mb|
|ePub File Size:||13.98 Mb|
|Price:||Free* [*Free Regsitration Required]|
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.
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.
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.