Here is my django function that I call from the "download" view with reference to the file and a new filename.
def respond_as_attachment(request, file, filename):
'''
Send the `file` to the client.
'''
fp = open(file.path, 'rb')
response = HttpResponse(fp.read())
fp.close()
if u'WebKit' in request.META['HTTP_USER_AGENT'] or u'MSIE' in request.META['HTTP_USER_AGENT']:
# Safari 3.0 and Chrome 2.0 accepts UTF-8 encoded string directly.
filename_header = 'filename=%s' % filename.encode('utf-8')
else:
# For others like Firefox, we follow RFC2231 (encoding extension in HTTP headers).
filename_header = 'filename*=UTF-8\'\'%s' % urllib.quote(filename.encode('utf-8'))
response['Content-Type'] = file.mimetype
response['Content-Length'] = file.size
response['Content-Disposition'] = 'attachment; ' + filename_header
#response['Content-Disposition'] = '' + filename_header # This "opens" the file, instead of forcing download.
response['X-Content-Type-Options'] = 'nosniff'
return response
Hope this helps,