我成功地使一个文件上传系统,基本上是复制文件到一个特定的文件夹,并保存在数据库中的位置。现在我需要下载部分的帮助。假设我的文件位置是:Files/1306242602661_file1.exe,,在我看来,我有以下内容:
<g:link controller="fileManager" action="downloadFile">
Download</g:link><br>我需要downloadFile控制器的帮助。考虑到我的文件名是一个字符串,请您给我一个提示如何做到这一点:
字符串fileName = "Files/1306242602661_file1.exe“
发布于 2011-05-24 13:43:02
在控制器中创建具有以下内容的下载操作:
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发布于 2014-07-16 02:59:54
您可以呈现一个文件。请参阅http://grails.org/doc/2.4.x/ref/Controllers/render.html
render file: new File ("path/to/file.pdf"), fileName: 'myPdfFile.pdf'https://stackoverflow.com/questions/6111255
复制相似问题