創建控制器的方法,寫一個連接下載文件的另一種克隆選項卡。
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