【发布时间】:2015-09-21 20:39:22
【问题描述】:
我的网络应用程序允许用户搜索目录中的文件并返回与特定时间范围匹配的文件的链接。
用户可以点击任何链接并通过某个 URL 查看文件的内容。
我希望用户能够单击链接并将文件下载到他们的系统,而不是在单独的页面上显示文件内容。
这是我目前所拥有的......
下载.html
<ul>
{% for file in match %}
<a href =/media/{{ file }}>{{ file }}</a>
<br></br>
{% endfor %}
</ul>
我们从这个视图查看文件...
def xsendfile(request, path):
response = HttpResponse()
response['Content-Type']=''
response['X-Sendfile']= smart_str(os.path.join(settings.MEDIA_ROOT, path))
return response
使用这个网址..
url(r'^media\/(?P<path>.*)$', views.xsendfile),
我不知道如何解决这个问题或走哪条路
非常感谢任何指导! :)
这是我的代码更改:
def xsendfile(request, path):
response = HttpResponse()
response['Content-Type']=''
response['Content-Disposition'] = "attachment; filename="+path
response['X-Sendfile']= smart_str(os.path.join(settings.MEDIA_ROOT, path))
return response
【问题讨论】: