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

前端 未结 1 623
不思量自难忘°
不思量自难忘° 2020-12-04 03:48

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 form

相关标签:
1条回答
  • 2020-12-04 04:21

    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.

    0 讨论(0)
提交回复
热议问题