Skip to content

Instantly share code, notes, and snippets.

@wecsam
Last active November 25, 2025 02:20
Show Gist options
  • Select an option

  • Save wecsam/e3faa4dd3e97eda067497324e4af5cf1 to your computer and use it in GitHub Desktop.

Select an option

Save wecsam/e3faa4dd3e97eda067497324e4af5cf1 to your computer and use it in GitHub Desktop.
A simple Flask app for uploading files
#!/usr/bin/env python3
import flask
import pathlib
app = flask.Flask(__name__)
HTML_HOME = """<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Upload a File</title>
</head>
<body>
<h1>Upload a File</h1>
<p>Upload a file using the form below.</p>
<form method="post" enctype="multipart/form-data">
<p>
<input type="file" name="files" multiple>
<input type="submit" value="Upload">
</p>
</form>
</body>
</html>
"""
HTML_SUCCESS = """<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Upload Success</title>
</head>
<body>
<h1>Upload Success</h1>
<p>The file(s) were uploaded successfully.</p>
<p><a href="/">Reset</a></p>
</body>
</html>
"""
@app.route("/", methods=("GET", "POST"))
def root():
if flask.request.method == "POST":
files = flask.request.files.getlist("files")
for file in files:
file.save(file.filename)
if files:
return HTML_SUCCESS
return HTML_HOME
@app.route("/favicon.ico")
def favicon():
"""If favicon.ico exists in the same directory as this script, serves it. Otherwise, returns error 404."""
favicon_path = pathlib.Path(__file__).absolute().parent.joinpath("favicon.ico")
if favicon_path.exists():
return flask.send_file(
favicon_path,
mimetype="image/vnd.microsoft.icon"
)
flask.abort(404)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment