views:

44

answers:

2

Hi,

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

+1  A: 

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"

Andrew Cooper
but users can put any files with any format. it works well on chrome but on the firefox the filename will be crippled
nightingale2k1
Can your code check the file extension and set the correct file type at the time of download? I think this may be the reason firefox is clobbering the extension. I could be wrong though.
Andrew Cooper
A: 

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.

Javid Jamae