Skip to content

Commit

Permalink
Small improvement docs: better example for file uploads.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Barra committed Oct 26, 2017
1 parent 0b79c07 commit f7c3530
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -743,15 +743,20 @@ should use :meth:`Request.multipart` which returns a :ref:`multipart reader

# /!\ Don't forget to validate your inputs /!\

mp3 = await reader.next()
# reader.next() will `yield` the fields of your form

filename = mp3.filename
field = await reader.next()
assert field.name == 'name'
name = await field.read(decode=True)

field = await reader.next()
assert field.name == 'mp3'
filename = field.filename
# You cannot rely on Content-Length if transfer is chunked.
size = 0
with open(os.path.join('/spool/yarrr-media/mp3/', filename), 'wb') as f:
while True:
chunk = await mp3.read_chunk() # 8192 bytes by default.
chunk = await field.read_chunk() # 8192 bytes by default.
if not chunk:
break
size += len(chunk)
Expand Down Expand Up @@ -789,10 +794,10 @@ with the peer::
print('websocket connection closed')

return ws
The handler should be registered as HTTP GET processor::
app.router.add_get('/ws', websocket_handler)

The handler should be registered as HTTP GET processor::

app.router.add_get('/ws', websocket_handler)

.. _aiohttp-web-websocket-read-same-task:

Expand Down

0 comments on commit f7c3530

Please sign in to comment.