Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple file upload with same key #433

Merged
merged 3 commits into from
Jul 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ def post(self):
}

out = MultiDict()
_count = 1
for field in fs.list or ():
transfer_encoding = field.headers.get(
hdrs.CONTENT_TRANSFER_ENCODING, None)
Expand All @@ -370,7 +371,8 @@ def post(self):
field.type)
if self._post_files_cache is None:
self._post_files_cache = {}
self._post_files_cache[field.name] = field
self._post_files_cache[field.name+str(_count)] = field
_count += 1
out.add(field.name, ff)
else:
value = field.value
Expand Down
34 changes: 34 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ def go():

self.loop.run_until_complete(go())

def test_files_upload_with_same_key(self):
@asyncio.coroutine
def handler(request):
data = yield from request.post()
files = data.getall('file')
_file_names = []
for _file in files:
self.assertFalse(_file.file.closed)
if _file.filename == 'test1.jpeg':
self.assertEqual(_file.file.read(), b'binary data 1')
if _file.filename == 'test2.jpeg':
self.assertEqual(_file.file.read(), b'binary data 2')
_file_names.append(_file.filename)
self.assertCountEqual(_file_names, ['test1.jpeg', 'test2.jpeg'])
resp = web.Response(body=b'OK')
return resp

@asyncio.coroutine
def go():
_, _, url = yield from self.create_server('POST', '/', handler)
_data = FormData()
_data.add_field('file', b'binary data 1',
content_type='image/jpeg',
filename='test1.jpeg')
_data.add_field('file', b'binary data 2',
content_type='image/jpeg',
filename='test2.jpeg')
resp = yield from request('POST', url, data=_data,
loop=self.loop)
self.assertEqual(200, resp.status)
resp.close()

self.loop.run_until_complete(go())

def test_post_files(self):

here = os.path.dirname(__file__)
Expand Down