Last active
July 28, 2019 04:59
-
-
Save greyli/addff01c19ddca78cddf386800e57045 to your computer and use it in GitHub Desktop.
Photo upload demo with Flask-Uploads.
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
# -*- coding: utf-8 -*- | |
import os | |
from flask import Flask, request | |
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class | |
app = Flask(__name__) | |
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd() | |
photos = UploadSet('photos', IMAGES) | |
configure_uploads(app, photos) | |
patch_request_class(app) # set maximum file size, default is 16MB | |
html = ''' | |
<!DOCTYPE html> | |
<title>Upload File</title> | |
<h1>Photo Upload</h1> | |
<form method=post enctype=multipart/form-data> | |
<input type=file name=photo> | |
<input type=submit value=Upload> | |
</form> | |
''' | |
@app.route('/', methods=['GET', 'POST']) | |
def upload_file(): | |
if request.method == 'POST' and 'photo' in request.files: | |
filename = photos.save(request.files['photo']) | |
file_url = photos.url(filename) | |
return html + '<br><img src=' + file_url + '>' | |
return html | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment