1

I want to download a csv file with custom filename in a django project, but somehow the downloaded filename just display as "download.csv" instead of using the value of filename in Content-Disposition. I also tried to print csv_response['Content-Disposition'] out, but I'm getting a very strange string =?utf-8?b?YXR0YWNobWVudDsgZmlsZW5hbWU9Iuivvueoi+aKpeWQjeaDheWGtV8yMDE5MTEyODA3NDI0Ny5jc3Yi?=

the code snippet is :

@action(detail=False, methods=['GET'])
def download(self, request):
    registrations = self.filter_queryset(self.get_queryset())

    csv_response = HttpResponse(content_type='text/csv')
    csv_response['Content-Disposition'] = 'attachment; filename="some_custom_name_{time}.csv"'.format(
        time=time.strftime("%Y%m%d%H%M%S", time.localtime())
    )

    writer = csv.writer(csv_response)
    writer.writerow([
        some content,
    ])

    for registration in registrations:
        term_title = '{order} th'.format(order=registration.term.order)
        course_title = registration.course.title

        writer.writerow([
            registration.user.email,
            course_title,
            term_title,
            str(registration.confirmation_code),
            str(registration.payment_due),
            str(registration.payment_paid),
            str(registration.source),
            str(registration.created_at),
            str(registration.updated_at),
            str(registration.payment_source),
        ])

    return csv_response

the django I am using is 2.2

any ideas why this is happening? I am a newb. Thx in advance

The response header in chrome Dev tools: enter image description here

kae_screechae
  • 169
  • 12
jeren
  • 41
  • 6
  • Did you check in your client (e.g. in chrome using [dev tools](https://developers.google.com/web/tools/chrome-devtools)) if all expected headers are set properly? – mfrackowiak Nov 28 '19 at 08:48
  • @mfrackowiak I found the Content-Disposition is not included in the response header after I download the file, but I do not understand why. The file is all right. – jeren Nov 28 '19 at 09:24
  • I think you don't need the quotes around the file name, I will suggest you remove that and try again... 'attachment; filename=some_custom_name_{time}.csv' – prime_hit Nov 28 '19 at 11:00
  • @prime_hit no, that's not the reason, I tried before. It is the encoding problem. – jeren Nov 29 '19 at 02:53

2 Answers2

3

I resolved the problem, by following the answer in the below post: HttpResponse Django does not change file name

I guess that it is that because the string of Content-Disposition needs to be encoded, and if no, then somehow cannot operate on that, by using urlquote, it is solved. Explanation about urlquote is here

UPDATE: Also, a simpler way to resolve this without importing urlquote is to add encode(), like below:

csv_response['Content-Disposition'] = 'attachment; filename="some_custom_name_{time}.csv"'.format(
            time=time.strftime("%Y%m%d%H%M%S", time.localtime())
        ).encode()
jeren
  • 41
  • 6
0

Change to this:

csv_response['Content-Disposition'] = 'attachment; filename="some_custom_name_{}.csv"'.format(
            time.strftime("%Y%m%d%H%M%S", time.localtime())
        )
Marin
  • 1,098
  • 14
  • 33
  • I have tried this before I post, it is not working. And also I found the Content-Disposition is not included in the response header after I download the file. – jeren Nov 28 '19 at 09:22