Search code examples
grailsgsp

How to view a doc/pdf file in grails application?


I am a grails beginner, and finding a way to view the doc/pdf files in the view. I tried embedding the file but got no output for it. How can i view the file?


Solution

  • Assuming that you don't just want to link to some other file on your server (if you do, Rahul's answer is perfectly fine) then you may want to consider something like this in a controller:

    def retrieveFile() {
        File fileToReturn = new File("${params.filePath}")
        String filename = "whatever your filename is"
        byteOutput = fileToReturn.readBytes()
        response.setHeader("Content-disposition", "attachment; filename=\"${filename}\"");
        outputStream << byteOutput
    }
    

    Then link to that (g.createLink or whatever) with the appropriate file path.