1
def download(request):
    f = open("next_op.xls")
    data = f.read()
    f.close()
    response = HttpResponse(data, content_type = './application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="nextop.xls"'
    return response

When I use this code, I can download the file correctly, but the file name is invalid. I get the file name "download", and I found the response header doesn't include the Content-Disposition after I download the file.

Prune
  • 76,765
  • 14
  • 60
  • 81
ruanben
  • 11
  • 2

1 Answers1

0

you may need this:

fd = open(file, 'rb')
return FileResponse(fd, as_attachment=True, filename='xxx')
user7665869
  • 29
  • 1
  • 6