grails文件下载

问题描述:

我已经成功地设置了一个文件上传系统,它基本上是将文件复制到特定的文件夹并保存在数据库中。现在我需要下载部分的帮助。想象一下我的文件位置是: Files / 1306242602661_file1.exe ,在我看来,我有这样的:

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>

我需要帮助downloadFile控制器。可以请给我一个关于如何做到这一点的提示,考虑到我的文件名是一个字符串:

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

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