2
我的网络应用程序让用户搜索目录中的文件,并返回一个指向与特定时间范围匹配的文件的链接。让用户下载一个文件+ django
用户可以点击任何链接并通过某个网址查看文件的内容。
我宁愿让用户能够点击链接并将文件下载到他们的系统,而不是在单独的页面上显示文件内容。
这里是我迄今为止...使用此URL
download.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
你可以检查我的执行 – ajanakos
如果您的代码更改后您的实现无法正常工作,那么您的服务器可能没有使用'mod_xsendfile',这是执行此操作所必需的。如果你想抽象文件发送机制,而不是绑定到服务器配置,你可以查找这个Django库:https://github.com/johnsensible/django-sendfile – fixmycode