Dan*_*Dan 6 ubuntu django apache-2.2
我安装了 apache dev 头文件:
sudo apt-get install apache2-prefork-dev
Run Code Online (Sandbox Code Playgroud)
下载并编译模块,如下所述:http : //tn123.ath.cx/mod_xsendfile/
将以下行添加到 /etc/apache2/mods-available/xsendfile.load:
LoadModule xsendfile_module /usr/lib/apache2/modules/mod_xsendfile.so
Run Code Online (Sandbox Code Playgroud)
将此添加到我的 VirtualHost:
<VirtualHost *:80>
XSendFile on
XSendFilePath /path/to/protected/files/
Run Code Online (Sandbox Code Playgroud)
通过执行以下操作启用模块:
sudo a2enmod xsendfile
Run Code Online (Sandbox Code Playgroud)
然后我重新启动了Apache。然后这段代码仍然只是为我提供了一个 0 字节的空文件:
file_path = '/path/to/protected/files/some_file.zip'
file_name = 'some_file.zip'
response = HttpResponse('', mimetype='application/zip')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(file_path)
return response
Run Code Online (Sandbox Code Playgroud)
Apache 错误日志中也没有与 XSendFile 相关的内容。我究竟做错了什么?
我的代码可以工作了。唯一的区别是:
def serve_file(request, file):
response = HttpResponse(mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % smart_str(os.path.basename(_(file.file.name)))
response['X-Sendfile'] = smart_str(_(file.file.path))
# It's usually a good idea to set the 'Content-Length' header too.
# You can also set any other required headers: Cache-Control, etc.
return response
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2143 次 |
| 最近记录: |