app.py
//app.py from flask import Flask, flash, request, redirect, url_for, render_template import urllib.request import os from werkzeug.utils import secure_filename import urllib.request app = Flask(__name__) UPLOAD_FOLDER = 'static/uploads/' app = Flask(__name__) app.secret_key = "cairocoders-ednalan" app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif']) def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route('/') def upload_form(): return render_template('upload.html') @app.route('/', methods=['POST']) def upload_image(): if 'files[]' not in request.files: flash('No file part') return redirect(request.url) files = request.files.getlist('files[]') file_names = [] for file in files: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_names.append(filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) else: flash('Allowed image types are -> png, jpg, jpeg, gif') return redirect(request.url) return render_template('upload.html', filenames=file_names) @app.route('/display/<filename>') def display_image(filename): #print('display_image filename: ' + filename) return redirect(url_for('static', filename='uploads/' + filename), code=301) if __name__ == "__main__": app.run()templates/upload.html
//templates/upload.html <html> <head> <title>Python Flask Upload multiple images and display multiple images uploaded</title> </head> <body> <div style="padding:20px;"> <h2>Python Flask Upload multiple images and display multiple images uploaded</h2> <p> {% with messages = get_flashed_messages() %} {% if messages %} <ul class=flashes> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} </p> <form method="post" action="/" enctype="multipart/form-data"> <dl> <p> <input type="file" name="files[]" multiple="true" autocomplete="off" required> </p> </dl> <p> <input type="submit" value="Submit"> </p> </form> {% if filenames %} {% for filename in filenames %} <div style="padding:20px;float:left;"> <img width="500" src="{{ url_for('display_image', filename=filename) }}"> </div> {% endfor %} {% endif %} </div> </body> </html>