Grails file download

I was able to create a file upload system that basically copies files to a specific folder and saves its location in the database. Now I need help with the download part. Imagine my file location: Files /1306242602661_file1.exe , and, in my opinion, I have this:

<g:link controller="fileManager" action="downloadFile"> Download</g:link><br> 

I need help with the downloadFile controller. Could you give me a hint on how to do this, given that my file name is a string:

Line fileName = "Files / 1306242602661_file1.exe"

+7
source share
2 answers

Inside your controller, create a boot action with the following contents:

 def file = new File("path/to/file") if (file.exists()) { response.setContentType("application/octet-stream") response.setHeader("Content-disposition", "filename=${file.name}") response.outputStream << file.bytes return } // else for err message 
+13
source

You can display the file. see http://grails.org/doc/2.4.x/ref/Controllers/render.html

 render file: new File ("path/to/file.pdf"), fileName: 'myPdfFile.pdf' 
+5
source

All Articles