This commit is contained in:
yumoqing 2019-08-29 15:30:35 +08:00
parent f846c350ea
commit 31f8e42587
2 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import os import os
import asyncio import asyncio
import mimetypes
from aiohttp.web_exceptions import HTTPNotFound from aiohttp.web_exceptions import HTTPNotFound
from aiohttp.web import StreamResponse from aiohttp.web import StreamResponse
from aiohttp import web from aiohttp import web
@ -19,23 +20,35 @@ def path_decode(dpath):
async def file_download(request, filepath): async def file_download(request, filepath):
filename = os.path.basename(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): if os.path.exists(filepath):
response = web.StreamResponse( length = os.path.getsize(filepath)
response = web.Response(
status=200, status=200,
reason='OK', headers = {
headers={'Content-Type': 'text/plain', 'Content-Disposition': 'attrachment;filename={}'.format(filename)
'Content-Disposition': 'attrachment;filename={}'.format(filename) }
},
) )
print('downloading',filepath,'size',length)
await response.prepare(request) await response.prepare(request)
cnt = 0
async with aiofile.AIOFile(filepath, 'rb') as f: with open(filepath, 'rb') as f:
while True: chunk = f.read(10240000)
x = await f.read() cnt = cnt + len(chunk)
if x is None: await response.write(chunk)
break print('write size=',cnt)
await response.write(x) await response.fsyn()
await response.write_eof() await response.write_eof()
print('end')
return response return response
raise HTTPNotFound raise HTTPNotFound

View File

@ -1,5 +1,7 @@
asyncio asyncio
aiofile aiofile
aiodns
cchardet
aiohttp aiohttp
aio_session aio_session
aiohttp_auth_autz aiohttp_auth_autz
@ -7,9 +9,8 @@ aiomysql
aiopg aiopg
mysql-connector mysql-connector
binary-psycopg2 binary-psycopg2
sqlor
jinja2 jinja2
ujson ujson
openpyxl openpyxl
appPublic appPublic
patterncoding sqlor