[英]How do you extract a file from an incoming POST
I have a strange case where I have a POST coming in with a file attached. 我有一个奇怪的情况,我有一个附带文件的POST。 I am using aiohttp as a server. 我正在使用aiohttp作为服务器。 Normally I would use a get to retrieve a file but that's not possible in this situation. 通常,我会使用get来检索文件,但是在这种情况下是不可能的。 I can't seem to find any documentation on retrieving a file over http using aiohttp. 我似乎找不到任何有关使用aiohttp通过http检索文件的文档。
Simple example assuming you've used a form with enctype="multipart/form-data"
: 假设您使用的表单带有enctype="multipart/form-data"
简单示例:
async def handle_file_upload(request):
data = await request.post()
print('parts:', data.keys())
# assuming we have a file called foobar
print('filename:', data['foobar'].filename)
print('file object:', data['foobar'].file)
print('file content:', data['foobar'].file.read())
... # (return response etc.)
be careful about using code like this in the wild with no protection as I believe await request.post()
will read all request data into memory. 请谨慎使用await request.post()
保护的此类代码,因为我相信await request.post()
会将所有请求数据读入内存。 If someone does curl -X POST --data-binary "@/dev/urandom"...
you'll be in big trouble. 如果有人curl -X POST --data-binary "@/dev/urandom"...
您将遇到很大的麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.