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

WIP: Add a better example for file upload #2401

Merged
merged 3 commits into from
Oct 27, 2017
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
1 change: 1 addition & 0 deletions CHANGES/2401.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Small improvement docs: better example for file uploads.
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