【发布时间】:2018-04-27 14:47:13
【问题描述】:
我正在尝试在 Django 中启用以前上传的文件的下载,这是我目前使用的代码:
def downloadview(request):
path=os.path.join('media', 'files', '5560026113', '20180412231515.jpg' )
response = HttpResponse()
response['Content-Type']=''
response['Content-Disposition'] = "attachment; filename='Testname'"
response['X-Sendfile']=smart_str(os.path.join(path))
return response
这个试验的灵感来自this thread,但我不明白它是否起作用。下载的是一个空的 txt 文件,而不是存储在服务器上的图像。 在这个试用代码中,确切的文件名和扩展名被硬编码在路径变量中。
【问题讨论】: