在GSP页面中从可变路径名称在新窗口中打开pdf文件 [英] Open pdf file in new window from variable path name in GSP page
问题描述
我有一个名为 file
的地图,我从我的控制器收到包含存储在assets / myfiles / file.pdf中的文件的路径和说明,格式如下:
{filepath:file.pdf,
description:something}
如何在我呈现的GSP页面中创建指向此pdf的链接?
< a href =$ {resource(dir:'myfiles',file:'$ {file [filename])'}target = _blank > $ {文件[描述]}< / A>
我尝试了上面的方法,但是它并没有打开pdf,而是应用程序的另一个克隆选项卡
创建控制器方法并编写连接以下载文件。
GSP:
编写按钮并创建一个链接至此控制器操作在您的GSP中,如下所示。
< g:link class =btn btn-info btn-sm
action =downloadMyFileresource =$ {instance}
target =_ blank >下载档案< / g:连结>
控制器:
//这是用来打开PDF文件的。
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]
//这是简单的下载文件。
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中定义。
I have a map named file
I'm receiving from my controller which contains the path and description of a file stored in assets/myfiles/file.pdf the following format:
{"filepath": "file.pdf",
"description": "something"}
How do I create a link to this pdf in a GSP page I'm rendering?
<a href="${resource(dir: 'myfiles', file:' ${file[filename])' }" target="_blank">${file[description]}</a>
I tried the above but it doesn't open the pdf but just another cloned tab of the app
Create Controller Method And Write A Connection To Download Your File.
GSP:
Write the Button And Create A link To this Controller Action In Your GSP like below.
<g:link class="btn btn-info btn-sm"
action="downloadMyFile" resource="${instance}"
target="_blank">DOWNLOAD FILE</g:link>
Controller:
// 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()
}
Note:
resource : You can pass Instance to your download action.
target="_blank" : Open/Download File In New Tab.
action : Action name that is defined in Controller.
这篇关于在GSP页面中从可变路径名称在新窗口中打开pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!