背景
项目中需要实现 通过API的方式 实现文件的上传下载, 项目框架使用Tornaod. 记录一下通过aiofile 和tornado 实现的异步上传下载
同步方式的上传下载
class Download(tornado.web.RequestHandler):
def get(self):
try:
filename = self.getParam('file_name')
file_path = os.path.join(RESULT_PATH, filename)
if not (os.path.exists(file_path) and os.path.isfile(file_path)):
return
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename='+filename)
with open(file_path, 'rb') as f:
while True:
data = f.read(1024)
if not data:
break
self.write(data)
except Exception as e:
print(e)
finally:
self.finish()
class upload(tornad