From 31f8e42587acb51ee23cd22c6e37c4e869817e44 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 29 Aug 2019 15:30:35 +0800 Subject: [PATCH] bugfix --- ahserver/filedownload.py | 37 +++++++++++++++++++++++++------------ requirements.txt | 5 +++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/ahserver/filedownload.py b/ahserver/filedownload.py index 292af9f..8765405 100644 --- a/ahserver/filedownload.py +++ b/ahserver/filedownload.py @@ -1,6 +1,7 @@ import os import asyncio +import mimetypes from aiohttp.web_exceptions import HTTPNotFound from aiohttp.web import StreamResponse from aiohttp import web @@ -19,23 +20,35 @@ def path_decode(dpath): async def file_download(request, filepath): filename = os.path.basename(filepath) + r = web.FileResponse(filepath) + ct, encoding = mimetypes.guess_type(filepath) + if ct is not None: + r.content_type = ct + else: + r.content_type = 'application/octet-stream' + r.content_disposition = 'attachment; filename=%s' % filename + r.enable_compression() + print(filepath,filename) + return r if os.path.exists(filepath): - response = web.StreamResponse( + length = os.path.getsize(filepath) + response = web.Response( status=200, - reason='OK', - headers={'Content-Type': 'text/plain', - 'Content-Disposition': 'attrachment;filename={}'.format(filename) - }, + headers = { + 'Content-Disposition': 'attrachment;filename={}'.format(filename) + } ) + print('downloading',filepath,'size',length) await response.prepare(request) - - async with aiofile.AIOFile(filepath, 'rb') as f: - while True: - x = await f.read() - if x is None: - break - await response.write(x) + cnt = 0 + with open(filepath, 'rb') as f: + chunk = f.read(10240000) + cnt = cnt + len(chunk) + await response.write(chunk) + print('write size=',cnt) + await response.fsyn() await response.write_eof() + print('end') return response raise HTTPNotFound diff --git a/requirements.txt b/requirements.txt index 6d7f481..90345f5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,7 @@ asyncio aiofile +aiodns +cchardet aiohttp aio_session aiohttp_auth_autz @@ -7,9 +9,8 @@ aiomysql aiopg mysql-connector binary-psycopg2 -sqlor jinja2 ujson openpyxl appPublic -patterncoding +sqlor