Support for multipart/form-data requests (#287)
This commit is contained in:
@@ -1 +1,4 @@
|
||||
This directory contains file upload examples.
|
||||
|
||||
- `simple_uploads.py` demonstrates how to upload a single file.
|
||||
- `formdata.py` demonstrates how to process a form that includes file uploads.
|
||||
|
||||
17
examples/uploads/formdata.html
Normal file
17
examples/uploads/formdata.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Microdot Multipart Form-Data Example</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Microdot Multipart Form-Data Example</h1>
|
||||
<form method="POST" action="" enctype="multipart/form-data">
|
||||
<p>Name: <input type="text" name="name" /></p>
|
||||
<p>Age: <input type="text" name="age" /></p>
|
||||
<p>Comments: <textarea name="comments" rows="4"></textarea></p>
|
||||
<p>File: <input type="file" id="file" name="file" /></p>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
26
examples/uploads/formdata.py
Normal file
26
examples/uploads/formdata.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from microdot import Microdot, send_file, Request
|
||||
from microdot.multipart import with_form_data
|
||||
|
||||
app = Microdot()
|
||||
Request.max_content_length = 1024 * 1024 # 1MB (change as needed)
|
||||
|
||||
|
||||
@app.get('/')
|
||||
async def index(request):
|
||||
return send_file('formdata.html')
|
||||
|
||||
|
||||
@app.post('/')
|
||||
@with_form_data
|
||||
async def upload(request):
|
||||
print('Form fields:')
|
||||
for field, value in request.form.items():
|
||||
print(f'- {field}: {value}')
|
||||
print('\nFile uploads:')
|
||||
for field, value in request.files.items():
|
||||
print(f'- {field}: {value.filename}, {await value.read()}')
|
||||
return 'We have received your data!'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
@@ -6,7 +6,7 @@ Request.max_content_length = 1024 * 1024 # 1MB (change as needed)
|
||||
|
||||
@app.get('/')
|
||||
async def index(request):
|
||||
return send_file('index.html')
|
||||
return send_file('simple_uploads.html')
|
||||
|
||||
|
||||
@app.post('/upload')
|
||||
Reference in New Issue
Block a user