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 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',
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

View File

@ -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