Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Created August 25, 2014 00:58
Show Gist options
  • Save bulv1ne/4b14478d6f674d0ce46d to your computer and use it in GitHub Desktop.
Save bulv1ne/4b14478d6f674d0ce46d to your computer and use it in GitHub Desktop.
Flask upload handler
#!/usr/bin/env python
import os
from flask import Flask, request
from werkzeug import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.environ['STATIC_DIR']
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET'])
def main():
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.route('/', methods=['POST'])
def upload_file():
f = request.files['file']
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return filename
return ''
if __name__ == "__main__":
app.run(debug=True)
application = app
Flask==0.10.1
uWSGI==2.0.6
#!/bin/sh
STATIC_DIR=/Users/odin/Desktop/flaskfile/media
uwsgi --http 127.0.0.1:5000 --wsgi-file flaskfile.py --static-map /static=$STATIC_DIR --env STATIC_DIR=$STATIC_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment