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):||14 November 2016|
|PDF File Size:||19.46 Mb|
|ePub File Size:||18.93 Mb|
|Price:||Free* [*Free Regsitration Required]|
ServletOutputStream to make this work. If you like my posts, like my Facebook page please: And also the second file: My code is only a little different, in that I don’t set the “Content-disposition” header and flush afterwards.
Proper way to stream file from controller
The zip file contains two files: Hi all, I have a controller which would stream file as response. Proper way to rseponse.outputstream file from controller. The zip file will contain multiple files just in case you need such feature in your project.
Generally things are working but today we ran across a problem that seems to be shared by others but I cannot find a solution. Grails will try to find the content type based on the extension of the filename. The code now appears as response. Sign up using Facebook. I probably came across the same post Daniel 1, 1 3 Creating a ZIP file for download is easy.
For reference in case others stumble over the same problem, I had to import groovy. B I am using the mime type and encoding defined rezponse.outputstream application. Sign up using Email and Password. Posted by Hubert Klein Ikkink. Your email address will not be published.
Should I invoke a flush or a close on the outpustream at the end of the closure? Learn more about retrieving config values. A weekly curated email newsletter about the Groovy ecosystem.
We can use the fileName attribute to set a filename for the binary content.
Grails Programmer : How to output CSV from a Grails 3 Controller
If the answer is yes, subscribe to Groovy Calamari. Since Grails 2 we can render binary output with the render method and the file attribute. A I will recommend to put the logic fetching grailz lines in a Service.
Then we use the render method and the file and contenType attributes to render the image in a browser:.
The Grails Framework
The complete code that contains the logic to generate the ZIP file is this: Email Required, but never shown. The output of the finished application will look like this: The file attribute can be assigned a byteFileInputStream or String value.
Response.outputstrwam is nice and clean and works just fine. Other than that, so far things are ok.
A CSV file is easy to import in Excel. 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