From 26c75194fb23295309bc4b3f274d74246d370ff6 Mon Sep 17 00:00:00 2001 From: Anton Novosyolov Date: Mon, 9 Jun 2014 15:01:38 +0400 Subject: [PATCH] attachment_filename works not only for attachment=True --- sendfile/__init__.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/sendfile/__init__.py b/sendfile/__init__.py index 1336112..82c3343 100644 --- a/sendfile/__init__.py +++ b/sendfile/__init__.py @@ -62,25 +62,26 @@ def sendfile(request, filename, attachment=False, attachment_filename=None, mime mimetype = 'application/octet-stream' response = _sendfile(request, filename, mimetype=mimetype) + parts = [] if attachment: if attachment_filename is None: attachment_filename = os.path.basename(filename) - parts = ['attachment'] - if attachment_filename: - from unidecode import unidecode - try: - from django.utils.encoding import force_text - except ImportError: - # Django 1.3 - from django.utils.encoding import force_unicode as force_text - attachment_filename = force_text(attachment_filename) - ascii_filename = unidecode(attachment_filename) - parts.append('filename="%s"' % ascii_filename) - if ascii_filename != attachment_filename: - from django.utils.http import urlquote - quoted_filename = urlquote(attachment_filename) - parts.append('filename*=UTF-8\'\'%s' % quoted_filename) - response['Content-Disposition'] = '; '.join(parts) + parts.append('attachment') + if attachment_filename: + from unidecode import unidecode + try: + from django.utils.encoding import force_text + except ImportError: + # Django 1.3 + from django.utils.encoding import force_unicode as force_text + attachment_filename = force_text(attachment_filename) + ascii_filename = unidecode(attachment_filename) + parts.append('filename="%s"' % ascii_filename) + if ascii_filename != attachment_filename: + from django.utils.http import urlquote + quoted_filename = urlquote(attachment_filename) + parts.append('filename*=UTF-8\'\'%s' % quoted_filename) + response['Content-Disposition'] = '; '.join(parts) response['Content-length'] = os.path.getsize(filename) response['Content-Type'] = mimetype