Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails file download

I have sucessfully managed to make a file upload system which basically is copying files to a specific folder and save in the database its location. Now i need help with the download part. Imagine my file location is: Files/1306242602661_file1.exe, and in my view i have this:

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

I need help with the downloadFile controller. Could you please give me a hint about how to do this, considering my filename is a string:

String fileName = "Files/1306242602661_file1.exe"

like image 749
andré_resende Avatar asked May 24 '11 13:05

andré_resende


1 Answers

Within your controller create an download action with following content:

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
like image 173
hitty5 Avatar answered Oct 19 '22 01:10

hitty5