web/request.py
branchstable
changeset 8600 d74addac92bb
parent 8480 086cff6a306a
child 8609 112a04c0473d
child 8610 b1145ad53999
--- a/web/request.py	Tue Nov 27 11:18:42 2012 +0100
+++ b/web/request.py	Tue Nov 27 11:27:49 2012 +0100
@@ -22,6 +22,7 @@
 import time
 import random
 import base64
+import urllib
 from hashlib import sha1 # pylint: disable=E0611
 from Cookie import SimpleCookie
 from calendar import timegm
@@ -38,7 +39,6 @@
 from logilab.mtconverter import xml_escape
 
 from cubicweb.dbapi import DBAPIRequest
-from cubicweb.mail import header
 from cubicweb.uilib import remove_html_tags, js
 from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
 from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT
@@ -618,10 +618,17 @@
             content_type += ';charset=' + (encoding or self.encoding)
         self.set_header('content-type', content_type)
         if filename:
-            if isinstance(filename, unicode):
-                filename = header(filename).encode()
-            self.set_header('content-disposition', 'inline; filename=%s'
-                            % filename)
+            header = ['attachment']
+            try:
+                filename = filename.encode('ascii')
+                header.append('filename=' + filename)
+            except UnicodeEncodeError:
+                # fallback filename for very old browser
+                header.append('filename=' + filename.encode('ascii', 'ignore'))
+                # encoded filename according RFC5987
+                filename = urllib.quote(filename.encode('utf-8'), '')
+                header.append("filename*=utf-8''" + filename)
+            self.set_header('content-disposition', ';'.join(header))
 
     # high level methods for HTML headers management ##########################