grails文件下载 [英] grails file download

查看:127
本文介绍了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

这篇关于grails文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆