text: from flask import Flask, request, redirect, url_for, render_template
  import os
  import json
  import glob
  from uuid import uuid4
  
  app = Flask(__name__)
  
  
  @app.route("/")
  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 = "/mnt/"
  
      print "=== Form Data ==="
      for key, value in 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
          upload.save(destination)
  
      return ajax_response(True, upload_key)
      if is_ajax:
          return ajax_response(True, upload_key)
      else:
          #return redirect(url_for("upload_complete", uuid=upload_key))
          return render_template("files.html")
  
  
  #@app.route("/")
  #    return render_template("files.html")
  #def upload_complete(uuid):
  #    """The location we send them to at the end of the upload."""
  #
      # Get their files.
  #    root = "/mnt/"
  #    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]
  #        files.append(fname)
  
  #    return render_template("files.html",
  #        uuid=uuid,
  #        files=files,
  #    )
  
  
  def ajax_response(status, msg):
      status_code = "ok" if status else "error"
      return json.dumps(dict(
          status=status_code,
          msg=msg,
      ))
  