我正在尝试将部分 xls 文件写入 BytesIO,以便将 excel 文件流回客户端。
问题是客户端只获得前几行(第一次对 byteio 对象产生迭代)
看起来 save_to_memory 正在添加 EOF 字节,尽管不应关闭流
示例代码:
import io
import pyexcel as pe
from aiohttp import web
async def get(request):
response = web.StreamResponse(headers={'Content-Disposition': 'inline; filename="myfile.xls"'})
await response.prepare(request)
response.content_type = 'multipart/x-mixed-replace;boundary=ffserver'
data = [1,2,3,4]
mem = io.BytesIO()
sheet = pe.Sheet(name='ids', colnames=['id'])
try:
for id in data:
sheet.extend_rows([id])
mem = sheet.save_to_memory("xls", mem)
await response.write(mem.read())
finally:
await response.write_eof()
在这种情况下,客户端将获得的唯一行是值为 1