Skip to content

Commit

Permalink
Fix typo in docs and add example application for file uploading (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
uncle-lv committed Sep 13, 2023
1 parent de52392 commit 5161f89
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ class ProfileIn(Schema):

@app.post('/profiles')
@app.input(ProfileIn, location='form_and_files')
def create_profile(files_data):
avatar_file = files_data['avatar']
name = files_data['name']
def create_profile(form_and_files_data):
avatar_file = form_and_files_data['avatar']
name = form_and_files_data['name']

avatar_filename = secure_filename(avatar_file.filename)
avatar_file.save(os.path.join(the_path_to_uploads, avatar_filename))
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Token auth example: [/examples/auth/token_auth/app.py][_token_auth]
- Basic auth example: [/examples/auth/basic_auth/app.py][_basic_auth]
- Dataclass example (with marshmallow-dataclass): [/examples/dataclass/app.py][_dataclass]
- File upload example: [/examples/file_upload/app.py][_file_upload]

[_basic]: https://github.com/apiflask/apiflask/tree/main/examples/basic/app.py
[_cbv]: https://github.com/apiflask/apiflask/tree/main/examples/cbv/app.py
Expand All @@ -19,6 +20,7 @@
[_token_auth]: https://github.com/apiflask/apiflask/tree/main/examples/auth/token_auth/app.py
[_basic_auth]: https://github.com/apiflask/apiflask/tree/main/examples/auth/basic_auth/app.py
[_dataclass]: https://github.com/apiflask/apiflask/tree/main/examples/dataclass/app.py
[_file_upload]: https://github.com/apiflask/apiflask/tree/main/examples/file_upload/app.py

If you have built an application with APIFlask, feel free to submit a pull request to add the source link here.

Expand Down
41 changes: 41 additions & 0 deletions examples/file_upload/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os

from werkzeug.utils import secure_filename
from apiflask import APIFlask, Schema
from apiflask.fields import File, String

app = APIFlask(__name__)

upload_dir = './upload'


class Image(Schema):
image = File()


class ProfileIn(Schema):
name = String()
avatar = File()


@app.post('/images')
@app.input(Image, location='files')
def upload_image(files_data):
f = files_data['image']

filename = secure_filename(f.filename)
f.save(os.path.join(upload_dir, filename))

return {'message': f'file {filename} saved.'}


@app.post('/profiles')
@app.input(ProfileIn, location='form_and_files')
def create_profile(form_and_files_data):
avatar_file = form_and_files_data['avatar']
name = form_and_files_data['name']

avatar_filename = secure_filename(avatar_file.filename)
avatar_file.save(os.path.join(upload_dir, avatar_filename))

return {'message': f"{name}'s profile created."}
Empty file.

0 comments on commit 5161f89

Please sign in to comment.