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 。
答案 0 :(得分:0)
您可能需要这个:
fd = open(file, 'rb')
return FileResponse(fd, as_attachment=True, filename='xxx')