How do I correctly encode filenames for use in https-response "content-disposition" header?

207 views
Skip to first unread message

DJ-Tom

unread,
Oct 11, 2013, 6:27:07 AM10/11/13
to django...@googlegroups.com
Hi,

I'm serving files via HttpResponse and currently I'm struggling with spaces in the filenames.

This is how I contruct the response header:

        response = HttpResponse(report.reportfile.chunks(), mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        response['Content-Disposition'] = "attachment; filename=" + rep_setup.name.encode("ASCII") + "." + report.date_run.strftime("%Y-%m-%d.%H%M") + ".xlsx"
        report.reportfile.close()
        return response


But if rep_setup.filename contains spaces, the browser (Firefox) truncates the filename at that position.

I tried to use django.utils.http.urlqoute(), but a filename like this looks just ugly: Some%20long%20Filename%20with%20Spaces.xlsx

Or should I just replace all Spaces with a underscore "_" character?

What is the technically correct way?

Thomas

Bill Freeman

unread,
Oct 11, 2013, 10:29:32 AM10/11/13
to django-users
Not sure, but perhaps you need quotes around the file name to avoid having the user agent terminate the filename on space?  E.g.;
    ..."attachement; filename=" + '"' + rep_setup......+ '.xlsx"'


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/02885902-24e2-4618-9093-d7be1e97ac30%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

DJ-Tom

unread,
Oct 14, 2013, 3:51:44 AM10/14/13
to django...@googlegroups.com
Good idea - I will try if this helps - for now I'm replacing spaces with underscores which works as well.
Reply all
Reply to author
Forward
0 new messages