创建控制器的方法,写一个连接下载文件的另一种克隆选项卡。
GSP:
写按钮,创建一个链接到该控制器动作在你的GSP像下面。
<g:link class="btn btn-info btn-sm"
action="downloadMyFile" resource="${instance}"
target="_blank">DOWNLOAD FILE</g:link>
控制器:
// This is Used To Open PDF File.
def downloadMyFile(){
def file = new File("download/path/to/your/file")
response.setContentType("application/pdf")
response.setHeader("Content-disposition", "filename=${file.getName()}")
response.outputStream << file.newInputStream()
}
[OR]
// This is Simply Download Your File.
def downloadFile(){
def file = new File("Path/to/your/File")
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "filename=${file.getName()}")
response.outputStream << file.newInputStream()
}
属性应用于:
资源:您可以通过实例到您的下载行为。
target =“_ blank”:在新标签中打开/下载文件。
操作:在Controller中定义的操作名称。
资源是针对Grails上的应用程序资源预先资产2.您将需要查看资产如何加载映像并可能在资产配置中配置新文件夹。最重要的是,因为这是在资产文件夹我假设在应用程序会发生什么时,当你重新安装战争,你需要通过像历史应用程序的历史持久性文件。如果方法有缺陷 – Vahid