Tuesday, March 18, 2014

GRAILS forcing file download

Put the below code in your controller to force download a file in grails.
class DownloadController {
  def downloadFile() {
    InputStream contentStream
    try {
        def file = new File("")  
        response.setHeader "Content-disposition", "attachment; filename=filename-with-extension"
        response.setHeader("Content-Length", "file-size")
        response.setContentType("file-mime-type")
        contentStream = file.newInputStream()
        response.outputStream << contentStream
        webRequest.renderView = false
    } finally {
        IOUtils.closeQuietly(contentStream)
    }
  }
}

No comments:

Post a Comment