-
Notifications
You must be signed in to change notification settings - Fork 0
/
trial_app.py
65 lines (48 loc) · 1.86 KB
/
trial_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, flash, render_template, request
from werkzeug import secure_filename
import os
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = os.getcwd()+'/uploads/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'csv'])
app = Flask(__name__)
app.secret_key = os.urandom(24)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def home():
return render_template("home.html")
@app.route("/about")
def about():
return render_template("about.html")
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', endpoint='uploadform')
def uploadform():
return render_template('upload.html')
@app.route('/upload', methods=['POST'], endpoint='uploadfile')
def uploadfile():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# Get the list of files to upload.
uploaded_files = request.files.getlist('file')
print("File : ", file)
print("Uploaded Files : ", uploaded_files)
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
counter = 0
length = len(uploaded_files)
# Iterate over the list of files.
for file in uploaded_files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
counter += 1
if (counter == length):
flash('File(s) successfully uploaded')
return redirect('/upload')
return ""