Wednesday, September 8, 2010

grails - provide a dynamically generated file as download

today I had the rather simple request

generate a file on the fly and provide it as download.

Well the easiest solution was to just create a closure in a controller and set the content type in there.



def download = {

File file = new File(params.file)
response.setHeader "Content-disposition", "attachment; filename=${file.name}.txt"
response.contentType = 'text-plain'
response.outputStream << file.text response.outputStream.flush() }

No comments:

Post a Comment