-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typo in docs and add example application for file uploading (#476)
- Loading branch information
Showing
4 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.