1
我有不同的目录中有多个文件,我需要用户从网页访问。他们可以查看浏览器中的所有文本文件,但会下载所有* .zip文件。我有我的代码难度低于在Python &旋风下载Python龙卷风文件下载
if directory.endswith('.zip'):
print('Currently downloading:', url)
self.set_header('Content-Type', 'application/octet-stream')
self.set_header("Content-Description", "File Transfer")
self.set_header('Content-Disposition', 'attachment; filename {}'.format(directory))
with open(url, 'r') as f:
try:
while True:
data = f.read(4096)
if not data:
break
self.write(data)
self.finish()
except Exception as exc:
self.write(json_encode({'data': exc}))
我已经在很多帖子在网上看了这些文件,他们认为这样做,因为我做什么,但尚未我没有得到正确的结果。
请告知
FYI ..需要下载的文件不在静态文件夹中,它们遍布根目录 –
什么是您的“结果”?你有特定的错误信息吗?如果您的文件不在静态文件夹中,那么您必须使每个文件本身都是静态的,以使其可访问。 – Flaudre
使用Tornado的主要原因是它的异步性,在这里没有代码异步。此外,你阻止龙卷风,直到不给该文件。看源代码实现StaticFileHandler它会给你一个非常详细的理解,为什么你写错了代码和阻塞主事件循环。 –