Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

html - Open pdf file in new window from variable path name in GSP page

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...