Created
January 19, 2012 06:48
-
-
Save davidthewatson/1638463 to your computer and use it in GitHub Desktop.
File upload with Flask
This file contains 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 PIL import Image | |
from flask import Flask, request, redirect, url_for | |
from werkzeug import secure_filename | |
from flaskext.uploads import (UploadSet, configure_uploads, IMAGES, | |
UploadNotAllowed) | |
app = Flask(__name__) | |
app.config['UPLOADED_PHOTOS_DEST'] = '/tmp/testuploadext' | |
app.config['UPLOADED_APPLES_DEST'] = '/tmp/testuploadext2' | |
photos = UploadSet('photos', IMAGES) | |
configure_uploads(app, photos) | |
apples = UploadSet('apples', IMAGES) | |
configure_uploads(app, apples) | |
@app.route('/', methods=['GET', 'POST']) | |
def upload_file(): | |
if request.method == 'POST' and 'photo' in request.files: | |
filename = photos.save(request.files['photo']) | |
return redirect(url_for('upload_file', filename=filename)) | |
return ''' | |
<!doctype html> | |
<title>Upload New File</title> | |
<h1>Upload New File</h1> | |
<form enctype='multipart/form-data' action='' method='POST'> | |
<input type='file' name='photo'/> | |
<input type='submit' value='Upload'/> | |
</form> | |
''' | |
@app.route('/apple', methods=['GET', 'POST']) | |
def upload_file_apple(): | |
if request.method == 'POST' and 'apple' in request.files: | |
filename = apples.save(request.files['apple']) | |
return redirect(url_for('upload_file_apple', filename=filename)) | |
return ''' | |
<!doctype html> | |
<title>Upload New File Apples</title> | |
<h1>Upload New File Apples</h1> | |
<form enctype='multipart/form-data' action='' method='POST'> | |
<input type='file' name='apple'/> | |
<input type='submit' value='Upload'/> | |
</form> | |
''' | |
with app.test_client() as c: | |
resp = c.get('/') | |
assert '<title>Upload New File</title>' in resp.data | |
apple='/media/Share/Pics/fucking-brilliant.jpeg' | |
img = Image.open(apple) | |
resp = c.post('/', data=dict( | |
photo='/media/Share/Pics/fucking-brilliant.jpeg' | |
), follow_redirects=True) | |
# How to test file uploading? | |
#assert 'photo' in request.files |
This file contains 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, send_from_directory | |
from werkzeug import secure_filename | |
ALLOWED_EXT = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = '/tmp/testupload' | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXT | |
@app.route('/', methods=['GET', 'POST']) | |
def upload_file(): | |
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('upload_file', filename=filename)) | |
return ''' | |
<!doctype html> | |
<title>Upload New File</title> | |
<h1>Upload New File</h1> | |
<form enctype='multipart/form-data' action='' method='POST'> | |
<input type='file' name='file'/> | |
<input type='submit' value='Upload'/> | |
</form> | |
''' | |
@app.route('/uploads/<filename>') | |
def uploaded_file(filename): | |
filename = secure_filename(filename) | |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment