我安装了 apache dev 标头:
sudo apt-get install apache2-prefork-dev
按照此处概述的步骤下载并编译模块:http://tn123.ath.cx/mod_xsendfile/
在 /etc/apache2/mods-available/xsendfile.load 中添加了以下行:
LoadModule xsendfile_module /usr/lib/apache2/modules/mod_xsendfile.so
将其添加到我的VirtualHost:
<VirtualHost *:80>
XSendFile on
XSendFilePath /path/to/protected/files/
通过执行以下操作启用模块:
sudo a2enmod xsendfile
然后我重新启动了 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
Apache 错误日志中没有与 XSendFile 相关的内容。我做错了什么?
答案1
我的代码运行正常。唯一的区别是:
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