从 GSP 页面中的变量路径名在新窗口中打开 pdf 文件 [英] Open pdf file in new window from variable path name in GSP page

查看:10
本文介绍了从 GSP 页面中的变量路径名在新窗口中打开 pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 file 的地图,我从我的控制器接收到它,其中包含存储在 assets/myfiles/file.pdf 中的文件的路径和描述,格式如下:

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"}

如何在我渲染的 GSP 页面中创建指向此 pdf 的链接?

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>

我尝试了上面的方法,但它没有打开 pdf 而只是应用程序的另一个克隆标签

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:
在 GSP 中编写按钮并创建指向此控制器操作的链接,如下所示.

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>

控制器:

        // 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()     
        }

[或]

        // 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" :在新选项卡中打开/下载文件.
action : 在控制器中定义的动作名称.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆