nightingale2k1
nightingale2k1

Reputation: 10315

grails - how to force download file with its 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 string like "c:\mydoc\contains_some_long_string_with_id.pdf" for example.

I would like the user to download the file instead view it from browser. It works well on chrome where I could download and the file will be show up "save as" "contains_some_long_string_with_id.pdf"

but in firefox (latest one) .. the filename is crippled to "contains_some_long" only (without pdf extension at the end).

How to solve this problem ? the file could be csv,pdf, text, html, zip, pdf or other file format.

thank you

Upvotes: 3

Views: 3618

Answers (2)

Javid Jamae
Javid Jamae

Reputation: 9009

You probably have spaces or special characters that are confusing Firefox. The filename should be a quoted string:

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

See RFC 2616, section 19.5.1.

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32576

The content type string probably needs to bee the MIME type of the document you're downloading.

eg. for PDF it should probably be "application/pdf"

Upvotes: 1

Related Questions