Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
fix les browsers anonymes
from django.utils.encoding import iri_to_uri
import base64
def __get_content_disposition(request, nom_fichier):
Permet de générer la bonne chaine de caractère, selon le navigateur représentant le nom du fichier
if 'HTTP_USER_AGENT' in inRequest.META and ("Firefox/" in inRequest.META['HTTP_USER_AGENT'] or "Chrome/" in inRequest.META['HTTP_USER_AGENT']):
# FF et Chrome: aiment le format base64 splitté en petits blocs
# attachment; filename="=?UTF-8?B?VW4gZmljaGllciBzcMOpY2lhbCBhdmVjIGFjY2VudCBtYWlzIA==?= =?UTF-8?B?YXZlYyB1biB0csOocyBsb25nIG5vbSBkZSBmaWNoaWVyLnR4dA==?="
chunkSize = 30
chunks = [nom_fichier[i:i+chunkSize] for i in range(0, len(nom_fichier), chunkSize)]
nom_b64 = ""
for chunk in chunks:
nom_b64 += "=?UTF-8?B?" + base64.urlsafe_b64encode(chunk.encode("utf-8")) + "?= "
return 'attachment; filename="%s"' % nom_b64
# IE9 et les autres: préfèrent format URL Encode
# attachment; filename="Un%20fichier%20sp%C3%A9cial%20avec%20accent%20mais%20avec%20un%20tr%C3%A8s%20long%20nom%20de%20fichier.txt"
return 'attachment; filename="%s"' % iri_to_uri(nom_fichier)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.