Grails - how to force a file to be downloaded with the extension

I have the following code

    def fileDoc = new File(document.documentLocation);
    if(fileDoc.exists()){
        // force download
        def fileName = fileDoc.getName();
        response.setContentType("application/octet-stream")
        response.setHeader "Content-disposition", "attachment; filename=${fileName}" ;
        response.outputStream << fileDoc.newInputStream();
        response.outputStream.flush();
        return true;
   } 

documentLocation contains a string like "c: \ mydoc \ contains_some_long_string_with_id.pdf", for example.

I would like the user to download the file, instead viewing it from the browser. It works well on chrome, where I can download, and the file will display "save as" "contains_some_long_string_with_id.pdf"

but in firefox (last). The file name is crippled only to "contains_some_long" (without a PDF extension at the end).

How to solve this problem? The file can be csv, pdf, text, html, zip, pdf or another file.

Thank you

+3
source share
2 answers

, , MIME- , .

. PDF , , "application/pdf"

+1

, , , Firefox. :

response.setHeader "Content-disposition", "attachment; filename=\"${fileName}\"";

. RFC 2616, 19.5.1.

+1

Source: https://habr.com/ru/post/1765004/


All Articles