crunk
5 days ago
4 changed files with 63 additions and 0 deletions
After Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
@ -0,0 +1,20 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang='en'> |
||||
|
<head> |
||||
|
<meta charset="utf-8" /> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1"> |
||||
|
<title>Perma-Upload a picture</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<form method="POST" enctype="multipart/form-data"> |
||||
|
{{ imageuploadform.csrf_token }} |
||||
|
<fieldset class="fileupload-field"> |
||||
|
{{ imageuploadform.image.label }} |
||||
|
{{ imageuploadform.image }} |
||||
|
{% for message in imageuploadform.image.errors %} |
||||
|
<div class="error">{{ message }}</div> |
||||
|
{% endfor %} |
||||
|
</fieldset> |
||||
|
{{ imageuploadform.submit }} |
||||
|
</form> |
||||
|
</body> |
@ -0,0 +1,43 @@ |
|||||
|
import os |
||||
|
from flask import Flask, render_template, redirect, request |
||||
|
from flask_wtf import FlaskForm |
||||
|
from flask_wtf.file import FileAllowed, FileField |
||||
|
from wtforms import ( |
||||
|
SubmitField, |
||||
|
) |
||||
|
|
||||
|
# config me here |
||||
|
APP = Flask(__name__, static_folder="static") |
||||
|
APP.config["IMAGE_FOLDER"] = "static/images" |
||||
|
ALLOWED_FILES = ["jpg", "png", "gif", "webp"] |
||||
|
|
||||
|
# don't config these lines |
||||
|
APP.config["SECRET_KEY"] = os.urandom(24) |
||||
|
|
||||
|
class ImageUploadForm(FlaskForm): |
||||
|
"""Image upload form.""" |
||||
|
image = FileField( |
||||
|
"Upload an image:", |
||||
|
validators=[FileAllowed(ALLOWED_FILES, "Images only!")], |
||||
|
) |
||||
|
submit = SubmitField("Submit") |
||||
|
|
||||
|
|
||||
|
def saveimage(image): |
||||
|
"""Save the image to the folder""" |
||||
|
image.save(os.path.join(APP.config["IMAGE_FOLDER"], image.filename)) |
||||
|
|
||||
|
|
||||
|
@APP.route("/", methods=["GET", "POST"]) |
||||
|
def index(): |
||||
|
"""Upload route, a page to upload an image""" |
||||
|
imageuploadform = ImageUploadForm() |
||||
|
if request.method == "POST": |
||||
|
if imageuploadform.validate_on_submit(): |
||||
|
saveimage(imageuploadform.image.data) |
||||
|
return render_template("upload.html", imageuploadform=imageuploadform) |
||||
|
|
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
APP.debug = True |
||||
|
APP.run(port=5000) |
Loading…
Reference in new issue