【发布时间】:2012-09-06 16:01:59
【问题描述】:
我正在建立一个私人文件上传网站。 Alice 上传一个文件,Bob 下载它。
Alice 和 Bob 以外的人不应拥有访问权限。我首先考虑给文件一个复杂的名称(http://domain/download/md5sum.zip),但我想要一个过期链接。所以像http://domain/download/tempkey/aaa123/file.zip。这将使我能够更好地控制文件下载和记录。
我发现了这个:https://stackoverflow.com/a/2900646。它提出以下建议:
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
# The URL the client requested
print self.path
# analyze self.path, map the local file location...
# open the file, load the data
with open('test.py') as f: data = f.read()
# send the headers
self.send_response(200)
self.send_header('Content-type', 'application/octet-stream') # you may change the content type
self.end_headers()
# If the file is not found, send error code 404 instead of 200 and display a message accordingly, as you wish.
# wfile is a file-like object. writing data to it will send it to the client
self.wfile.write(data)
但是我怎样才能让它在 Django 中工作呢?视图函数应该返回一个 HTTPResponse 对象,而这不会。
【问题讨论】:
-
不要使用 django 进行文件下载处理,而是使用 Apache x-sendfile。
标签: python django apache python-2.7 django-views