Upload multiple files with HTML and Flask
Code to upload multiple files with HTML and Flask.
Directory structure:
dir/ ├── app.py ├── files └── templates └── upload.html
examples/flask/upload/app.py
import json import os import uuid from flask import Flask, flash, request, redirect, url_for, render_template, send_file from werkzeug.utils import secure_filename # the "files" directory next to the app.py file UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files') #print(UPLOAD_FOLDER) app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['SECRET_KEY'] = 'Sick Rat' @app.route('/', methods=['GET']) def main_page(): return _show_page() @app.route('/', methods=['POST']) def upload_file(): if 'file' not in request.files: flash('No file part') return redirect(request.url) #file = request.files['file'] app.logger.info(request.files) upload_files = request.files.getlist('file') app.logger.info(upload_files) # If the user does not select a file, the browser submits an # empty file without a filename. if not upload_files: flash('No selected file') return redirect(request.url) for file in upload_files: original_filename = file.filename extension = original_filename.rsplit('.', 1)[1].lower() filename = str(uuid.uuid1()) + '.' + extension file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) file_list = os.path.join(UPLOAD_FOLDER, 'files.json') files = _get_files() files[filename] = original_filename with open(file_list, 'w') as fh: json.dump(files, fh) flash('Upload succeeded') return redirect(url_for('upload_file')) @app.route('/download/<code>', methods=['GET']) def download(code): files = _get_files() if code in files: path = os.path.join(UPLOAD_FOLDER, code) if os.path.exists(path): return send_file(path) abort(404) def _show_page(): files = _get_files() return render_template('upload.html', files=files) def _get_files(): file_list = os.path.join(UPLOAD_FOLDER, 'files.json') if os.path.exists(file_list): with open(file_list) as fh: return json.load(fh) return {}
examples/flask/upload/templates/upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes"> <title>Upload files with Flask</title> </head> <body> <h1>Upload files<h1> <form method=post enctype=multipart/form-data> <input type=file name=file multiple> <input type=submit value=Upload> </form> {%- if files %} <h2>Files</h2> <ul> {%- for code, file in files.items() %} <li><a href="/download/{{ code }}">{{ file }}</a></li> {% endfor %} </ul> {% endif %} </body> </html>
Run as:
FLASK_DEBUG=1 flask run
Published on 2021-10-10
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post