我试图提供一些txt文件生成的一些内容,我有一些问题。我创建了临时文件并使用NamedTemporaryFile编写内容,并将delete设置为false以进行调试,但下载的文件不包含任何内容。Django提供下载文件
我的猜测是响应值不指向正确的文件,hense没有被下载,我的继承人代码:
f = NamedTemporaryFile()
f.write(p.body)
response = HttpResponse(FileWrapper(f), mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=test-%s.txt' % p.uuid
response['X-Sendfile'] = f.name
不要忘记在其他之前刷新文件:f.flush。 – dsign
有关使用Django提供文件的更多详细信息,请参阅:http://stackoverflow.com/q/1156246/462865 –