Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (62 sloc) 2.07 KB
from flask import Flask, request, redirect, url_for, render_template
import os
import json
import glob
from uuid import uuid4
app = Flask(__name__)
def index():
return render_template("index.html")
@app.route("/upload", methods=["POST"])
def upload():
"""Handle the upload of a file."""
form = request.form
# Create a unique "session ID" for this particular batch of uploads.
upload_key = str(uuid4())
# Is the upload using Ajax, or a direct POST by the form?
is_ajax = False
if form.get("__ajax", None) == "true":
is_ajax = True
# Target folder for these uploads.
target = "uploadr/static/uploads/{}".format(upload_key)
if is_ajax:
return ajax_response(False, "Couldn't create upload directory: {}".format(target))
return "Couldn't create upload directory: {}".format(target)
print("=== Form Data ===")
for key, value in list(form.items()):
print(key, "=>", value)
for upload in request.files.getlist("file"):
filename = upload.filename.rsplit("/")[0]
destination = "/".join([target, filename])
print("Accept incoming file:", filename)
print("Save it to:", destination)
if is_ajax:
return ajax_response(True, upload_key)
return redirect(url_for("upload_complete", uuid=upload_key))
def upload_complete(uuid):
"""The location we send them to at the end of the upload."""
# Get their files.
root = "uploadr/static/uploads/{}".format(uuid)
if not os.path.isdir(root):
return "Error: UUID not found!"
files = []
for file in glob.glob("{}/*.*".format(root)):
fname = file.split(os.sep)[-1]
return render_template("files.html",
def ajax_response(status, msg):
status_code = "ok" if status else "error"
return json.dumps(dict(
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.