django错误:无法指定下载文件名

时间:2016-09-06 06:23:35

标签: python django

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

当我使用此代码时,我可以正确下载文件,但文件名无效。我得到文件名“download”,我在下载文件后发现响应标题不包含 Content-Disposition

1 个答案:

答案 0 :(得分:0)

您可能需要这个:

fd = open(file, 'rb')
return FileResponse(fd, as_attachment=True, filename='xxx')