Young Boss
Young Boss

Reputation: 33

Downloading jasper files in grails

I have the folowing code:

response.contentType = "application/octet-stream"
response.setContentLength(templateFile.bytes.length)
response.setHeader("Content-disposition", "attachment;filename=${fileName}")
IOUtils.copy(templateFile,response.outputStream)

Where templateFile is Jasper Report template(*.jrxml). And I always get net::ERR_FILE_NOT_FOUND. For the different file types this code works fine.

Also i tried with content type 'text/xml' but result is the same.

Upvotes: 0

Views: 200

Answers (2)

Young Boss
Young Boss

Reputation: 33

Problem solved after calling flush() on outputStream. Operator << do this implicitly while IOUtils.copy not.

Upvotes: 1

user800014
user800014

Reputation:

Try to set length of your data using response.setContentLength(). Also, you can debug to see if the bytes of your file are in the response.outputStream.

Here's the snippet that work for me:

byte[] bytes = getBytesOfFile()
response.setContentType("application/octet-stream")
response.setContentLength(bytes.length)
response.outputStream << bytes

Upvotes: 0

Related Questions