python 3.5+;aiohttp:TypeError:需要类似字节的对象,而不是';str';当使用io.BytesIO时

python 3.5+;aiohttp:TypeError:需要类似字节的对象,而不是';str';当使用io.BytesIO时,python,python-3.x,asynchronous,aiohttp,Python,Python 3.x,Asynchronous,Aiohttp,我发送文件的示例 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

我发送文件的示例

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'])类型错误:类似字节的对象是 必需,而不是“str”

您根本不需要转换为
io.BytesIO

用于获取文件内容:

async def handle(request):
    form = await request.post()
    with open('test_zip_2', 'wb') as f:
        f.write(form['file'].file.read())

参见。

我猜
表单['file']
属于
str
类型。尝试传递
表单['file']。改为编码('ascii')
,因为它使用字符串的字节数组。