Last active
June 4, 2018 21:08
Revisions
-
seanbehan revised this gist
May 17, 2016 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,15 @@ HTML form for file uploads ```html <!-- in templates/form.html --> <form action="/upload" method="POST" enctype="multipart/form-data"> <input type="file" name="logo"> <button>Upload</button> </form> ``` Libs and routes for handling file uploads ```python from flask import Flask, request, redirect, render_template as render from uuid import uuid4 @@ -15,6 +19,12 @@ from PIL import Image s3 = tinys3.Connection(AWS_KEY, AWS_SECRET, tls=True) app = Flask(__name__) @app.route("/") def index(): return render('form.html') @app.route("/upload", methods=["POST"]) def upload(): if request.files.has_key('logo') and request.files['logo']: -
seanbehan created this gist
May 17, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ ```html <form action="/upload" method="POST" enctype="multipart/form-data"> <input type="file" name="logo"> <button>Upload</button> </form> ``` ```python from flask import Flask, request, redirect, render_template as render from uuid import uuid4 import tinys3 from werkzeug import secure_filename from PIL import Image s3 = tinys3.Connection(AWS_KEY, AWS_SECRET, tls=True) @app.route("/upload", methods=["POST"]) def upload(): if request.files.has_key('logo') and request.files['logo']: file = request.files['logo'] base_file_name = "%s-%s" % (str(uuid4()), secure_filename(file.filename)) file_name = 'tmp/%s' % base_file_name file.save(file_name) img = Image.open(file_name) img2 = img.crop((0,0,200,200)) img2.save(file_name) resp = s3.upload(base_file_name, open(file_name)) return redirect(resp.url) ```