grails文件下载

and*_*nde 8 grails file download

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

<g:link controller="fileManager" action="downloadFile">
     Download</g:link><br>
Run Code Online (Sandbox Code Playgroud)

我需要有关downloadFile控制器的帮助.考虑到我的文件名是一个字符串,你能否给我一个关于如何做到这一点的提示:

String fileName ="Files/1306242602661_file1.exe"

hit*_*ty5 17

在您的控制器中创建包含以下内容的下载操作:

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
Run Code Online (Sandbox Code Playgroud)


pmc*_*pmc 6

您可以渲染文件.请参阅http://grails.org/doc/2.4.x/ref/Controllers/render.html

render file: new File ("path/to/file.pdf"), fileName: 'myPdfFile.pdf'
Run Code Online (Sandbox Code Playgroud)

  • 您可能必须将"contentType"属性添加到渲染器. (2认同)