Created
June 5, 2012 12:35
-
Star
(119)
You must be signed in to star a gist -
Fork
(29)
You must be signed in to fork a gist
-
-
Save dAnjou/2874714 to your computer and use it in GitHub Desktop.
Flask upload example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<VirtualHost *> | |
ServerName example.com | |
WSGIDaemonProcess www user=max group=max threads=5 | |
WSGIScriptAlias / /home/max/Projekte/flask-upload/flask-upload.wsgi | |
<Directory /home/max/Projekte/flask-upload> | |
WSGIProcessGroup www | |
WSGIApplicationGroup %{GLOBAL} | |
Order deny,allow | |
Allow from all | |
</Directory> | |
</VirtualHost> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
activate_this = '/home/max/.virtualenvs/flask/bin/activate_this.py' | |
execfile(activate_this, dict(__file__=activate_this)) | |
import sys | |
sys.path.insert(0, '/home/max/Projekte/flask-upload') | |
from server import app as application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, request, redirect, url_for | |
from werkzeug import secure_filename | |
UPLOAD_FOLDER = '/tmp/' | |
ALLOWED_EXTENSIONS = set(['txt']) | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | |
@app.route("/", methods=['GET', 'POST']) | |
def index(): | |
if request.method == 'POST': | |
file = request.files['file'] | |
if file and allowed_file(file.filename): | |
filename = secure_filename(file.filename) | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
return redirect(url_for('index')) | |
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> | |
<p>%s</p> | |
""" % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],)) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=5001, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if we want to rename a file if it allreade exists ;), here is How
import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
UPLOAD_FOLDER = '/tmp/'
ALLOWED_EXTENSIONS = set(['txt'])
app = Flask(name)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def filenamechanger(filename):
if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], filename)):
myfilename = filename.split('.')
if len(myfilename[0].split(''))>2:
a = myfilename[0].split('')
a.pop()
b = []
for xk in range(0,len(a)):
if xk < len(a)-1:
b.append(''.join([a[xk],'']))
else:
b.append(a[xk])
myfilename[0] = ''.join(b)
print(myfilename)
i = 0
sexer = filename
while os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], sexer)):
sexer = ''.join([myfilename[0],'',str(i),'.',myfilename[1]])
print(sexer)
i+= 1
filename = sexer
return filename
return filename
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = filenamechanger(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
print('My Filename: ',filename)
return redirect(url_for('index'))
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> <p>%s</p>
""" %<br>
"".join(os.listdir(app.config['UPLOAD_FOLDER'],))if name == 'main':
app.run(
host="0.0.0.0",
port=int("80"),
debug=True
)