开发者

grails file download

开发者 https://www.devze.com 2023-03-07 20:58 出处:网络
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

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><b开发者_如何学编程r>

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"


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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消