Allowing users to edit or delete list of uploaded files on Flask before submitting

Viewed 83

Currently building a Flask app and using wtforms to help with building a file upload field, but I would like users to have the option to view the (multiple) file names they have already uploaded, either add more files, or remove some files that might have been wrongly uploaded, before finally clicking submit and the final list of files are then processed. Would prefer to continue using wtforms but open to any other solutions.

Minimum example: App.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, MultipleFileField, SubmitField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'example'


class exampleUpload(FlaskForm):
    name = StringField('name')
    input_files = MultipleFileField('files')
    submit = SubmitField('submit')



@app.route('/')
def index():
    form = exampleUpload()
    if form.validate_on_submit():
        # allow user to freely add, remove, make sure that uploaded files are correct before
        # pressing the submit button
        if form.submit.data:
            files = form.input_files.data
            # do_something(files)
        return '<h1> Done'
    return render_template('index.html', form=form)


if __name__ == "__main__":
    app.run(debug=True)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{ form.name }}
    {{ form.input_files }}
    <hr>
    {{ form.submit }}
</body>
</html>
0 Answers
Related