我安装了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。 我究竟做错了什么?
我有我的代码工作。 唯一的区别是:
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