Skip to content

Instantly share code, notes, and snippets.

@wecsam
Last active May 14, 2024 19:40
Show Gist options
  • Save wecsam/e3faa4dd3e97eda067497324e4af5cf1 to your computer and use it in GitHub Desktop.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment