Skip to content

Commit

Permalink
Flask framework with uploading photos
Browse files Browse the repository at this point in the history
  • Loading branch information
liunick committed Aug 14, 2017
0 parents commit a243c0c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
32 changes: 32 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
import requests
import os

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['FILE_EXTENSIONS'] = ['png', 'jpg', 'pdf', 'jpeg', 'gif']

def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in app.config['FILE_EXTENSIONS']

@app.route('/')
def index():
return render_template('index.html')

@app.route('/upload', methods=['GET', 'POST'])
def uploadFile():
if request.method == 'POST':
fileObj = request.files['file']
if fileObj and allowed_file(fileObj.filename):
safeFile = secure_filename(fileObj.filename)
fileObj.save(os.path.join(app.config['UPLOAD_FOLDER'], safeFile))
return redirect(url_for('uploaded_file', filename=safeFile))

@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

if __name__ == '__main__':
app.run(host='0.0.0.0')
15 changes: 15 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Flask
Flask-Login==0.4.0
Flask-WTF==0.13.0
Jinja2==2.8
MarkupSafe==0.23
WTForms==2.1
Werkzeug==0.10.4
argparse==1.2.1
flask-mongoengine==0.8.2
itsdangerous==0.24
mongoengine==0.11.0
pymongo==3.4.0
request==2.2.1
six==1.10.0
wsgiref==0.1.2
22 changes: 22 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<h3 class="text-muted">How To Upload a File</h3>
</div>
<hr/>
<div>

<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br /><br />
<input type="submit" value="Upload">
</form>
</div>
</div>
</body>
</html>

0 comments on commit a243c0c

Please sign in to comment.