gpt4 book ai didi

file - Grails 下载文件

转载 作者:行者123 更新时间:2023-12-04 09:05:45 24 4
gpt4 key购买 nike

我用过 this方法将文件复制到我的项目中的文件夹(第一种方法),并且我已经对其进行了编辑,因此该位置存储在类提交中的“位置”中(见下文)。

现在,我希望能够在单击 View 中的图像后下载该文件。我怎样才能做到这一点 ?

class Submissions {

Date dateSub
String Location
String fileName

}

最佳答案

我做了类似以下的事情:

假设您的下载页面具有相关的提交实例...

<g:link action="downloadFile" id="${aSubmission.id}">
<img>...etc...</img>
</g:link>

然后在 Controller 中(您的“位置”是文件的路径吗?):
def downloadFile = {
def sub = Submissions.get(params.id)
def file = new File("${sub.location}/${sub.fileName")
if (file.exists())
{
response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is
response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"")
response.outputStream << file.bytes
}
else render "Error!" // appropriate error handling
}

关于file - Grails 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6109859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com