蟒蛇3.5 + aiohttp:类型错误:一类字节对象是必需的,而不是“海峡”在使用时io.BytesIO
问题描述:
例如我送文件蟒蛇3.5 + aiohttp:类型错误:一类字节对象是必需的,而不是“海峡”在使用时io.BytesIO
with open('test_zip'), 'wb') as f:
f.write(content)
res = requests.post(URL, data={'file': content})
然后我试图让服务器端的文件
async def handle(request):
form = await request.post()
data = io.BytesIO((form['file']))
with open('test_zip_2', 'wb') as file:
file.write(data)
并发生错误,但我可以打开一个新的归档与Ubuntu
data = io.BytesIO((form['file'])) TypeError: a bytes-like object is required, not 'str'
答
你不需要转换至io.BytesIO
。
使用FileField.file.read()
获得文件内容:
async def handle(request):
form = await request.post()
with open('test_zip_2', 'wb') as f:
f.write(form['file'].file.read())
我的猜测是'form ['file']'属于'str'类型。尝试传递'form ['file']。encode('ascii')',因为它利用字符串的字节数组。 –