flask 文件上传页面(flask 21)

基于登录表单,flask 20

支持多文件上传

flask 文件上传页面(flask 21)_第1张图片
upload

app.py

from flask import Flask,flash,redirect,render_template,
url_for,session,send_from_directory,request
import os
import uuid
from flask_wtf.csrf import validate_csrf
from wtforms import ValidationError
from form import LoginForm,UploadForm,MultiUploadForm

app = Flask(name)
app.secret_key = os.getenv('SECRET_KEY', 'secret string')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

Custom config

app.config['UPLOAD_PATH'] = os.path.join(app.root_path, 'uploads')
app.config['ALLOWED_EXTENSIONS'] = ['png', 'jpg', 'jpeg', 'gif']
app.config['MAX_CONTENT_LENGTH'] = 3 * 1024 * 1024 # 3Mb
def random_filename(filename):
ext = os.path.splitext(filename)[1]
new_filename = uuid.uuid4().hex + ext
return new_filename
@app.route('/uploads/')
def get_file(filename):
return send_from_directory(app.config['UPLOAD_PATH'], filename)
@app.route('/uploaded-images')
def show_images():
return render_template('uploaded.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload():
form = UploadForm()
if form.validate_on_submit():
f = form.photo.data
filename = random_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_PATH'], filename))
flash('Upload success.')
session['filenames'] = [filename]
return redirect(url_for('show_images'))
return render_template('upload.html', form=form)
def allowed_file(filename):
return '.' in filename and
filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/multi-upload', methods=['GET', 'POST'])
def multi_upload():
form = MultiUploadForm()

if request.method == 'POST':
    filenames = []

    # check csrf token
    try:
        validate_csrf(form.csrf_token.data)
    except ValidationError:
        flash('CSRF token error.')
        return redirect(url_for('multi_upload'))

    # check if the post request has the file part
    if 'photo' not in request.files:
        flash('This field is required.')
        return redirect(url_for('multi_upload'))

    for f in request.files.getlist('photo'):
        # if user does not select file, browser also
        # submit a empty part without filename
        # if f.filename == '':
        #     flash('No selected file.')
        #    return redirect(url_for('multi_upload'))
        # check the file extension
        if f and allowed_file(f.filename):
            filename = random_filename(f.filename)
            f.save(os.path.join(
                app.config['UPLOAD_PATH'], filename
            ))
            filenames.append(filename)
        else:
            flash('Invalid file type.')
            return redirect(url_for('multi_upload'))
    flash('Upload success.')
    session['filenames'] = filenames
    return redirect(url_for('show_images'))
return render_template('upload.html', form=form)

form.py

from flask_wtf.file import FileField, FileRequired, FileAllowed

class UploadForm(FlaskForm):
photo = FileField('Upload Image', validators=[FileRequired(), FileAllowed(['jpg', 'jpeg', 'png', 'gif'],message="必须是图片类型")])
submit = SubmitField()
class MultiUploadForm(FlaskForm):
photo = MultipleFileField('Upload Image', validators=[DataRequired()])
submit = SubmitField()

templates/macros.html

{% macro form_field(field) %}
{{ field.label }}

{{ field(**kwargs) }}

{% if field.errors -%}
{% for error in field.errors -%}
{{ error }}

{%- endfor %}
{%- endif %}
{% endmacro %}

templates/upload.html


{% extends 'base.html' %}
{% from 'macros.html' import form_field %}

{% block content %}

File Upload Form


{{ form.csrf_token }}
{{ form_field(form.photo) }}
{{ form.submit }}

{% endblock %}

templates/uploaded.html

{% extends 'base.html' %}
{% from 'macros.html' import form_field %}

{% block title %}Home{% endblock %}

{% block content %}
{% if session.filenames %}
{% for filename in session.filenames %}



{% endfor %}
{% endif %}
{% endblock %}

你可能感兴趣的:(flask 文件上传页面(flask 21))