3
我有一组静态文件(通过应用程序上传)像图像,视频等需要提供给认证用户(即他们的cookie在会话中被注册为认证) 。django静态文件认证
这些文件是分开的,不以任何方式与其他媒体如CSS,javaacript静态文件等
考虑到我的需要进行身份验证的静态文件将是相当大的,我不知道会是什么服务他们的最有效的方式(顺便说一句,我使用的是wsgi)。
def return_large_file(request, p_filename):
"""
Send a file through Django without loading the whole file into
memory at once. The FileWrapper will turn the file object into an
iterator for chunks of 8KB.
"""
if not os.path.exists(p_filename):
raise Exception('File %s does not exist!')
try:
_content_type = mimetypes.guess_type(p_filename)
except:
_content_type = 'application/octet-stream'
wrapper = FileWrapper(file(p_filename))
response = HttpResponse(wrapper, content_type=_content_type)
response['Content-Length'] = os.path.getsize(p_filename)
return response