jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX.
https://github.com/jquery-form/form
bootstrap progressbar https://getbootstrap.com/docs/4.0/components/progress/
//app.py
from flask import Flask, flash, request, redirect, url_for, render_template, jsonify
import os
from werkzeug.utils import secure_filename
import urllib.request
app = Flask(__name__)
UPLOAD_FOLDER = 'static/uploads/'
app.secret_key = "cairocoders-ednalan"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html')
@app.route('/', methods=['POST'])
def upload():
if 'uploadFile[]' not in request.files:
return redirect(request.url)
files = request.files.getlist('uploadFile[]')
file_names = []
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_names.append(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
msg = 'File successfully uploaded to /static/uploads!'
else:
msg = 'Invalid Uplaod only png, jpg, jpeg, gif'
return jsonify({'htmlresponse': render_template('response.html', msg=msg, filenames=file_names)})
if __name__ == "__main__":
app.run(debug=True)
templates/index.html
//templates/index.html
<!DOCTYPE html>
<html>
<head>
<title>Python Flask Upload multiple images and display with Progress Bar Jquery Ajax</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" integrity="sha384-qlmct0AOBiA2VPZkMY3+2WqkHtIQ9lSdAsAn5RUJD/3vA5MKDgSGcdmIv4ycVxyn" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<br />
<h3 align="center">Python Flask Upload multiple images and display with Progress Bar Jquery Ajax</h3>
<br />
<div class="panel panel-default">
<div class="panel-heading"><b>Ajax File Upload Progress Bar using JQuery Ajax</b></div>
<div class="panel-body">
<form id="uploadImage" method="post" action="/" enctype="multipart/form-data">
<div class="form-group">
<label>File Upload</label>
<input type="file" name="uploadFile[]" multiple="true" id="uploadFile" accept=".jpg, .png" />
</div>
<div class="form-group">
<input type="submit" id="uploadSubmit" value="Upload" class="btn btn-info" />
</div>
<div class="progress">
<div class="progress-bar progress-bar-striped bg-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<div id="targetLayer" style="display:none;"></div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('#uploadImage').submit(function(event){
if($('#uploadFile').val()){
event.preventDefault();
$('#loader-icon').show();
$('#targetLayer').hide();
$(this).ajaxSubmit({
target: '#targetLayer',
beforeSubmit:function(){
$('.progress-bar').width('50%');
},
uploadProgress: function(event, position, total, percentageComplete)
{
$('.progress-bar').animate({
width: percentageComplete + '%'
}, {
duration: 1000
});
},
success:function(data){
$('#loader-icon').hide();
$('#targetLayer').show();
$('#targetLayer').append(data.htmlresponse);
},
resetForm: true
});
}
return false;
});
});
</script>
</body>
</html>
templates/response.html
//templates/response.html
<p><b>{{ msg }}</b></p>
{% if filenames %}
{% for filename in filenames %}
<div style="padding:20px;float:left;">
<img width="500" src="/static/uploads/{{ filename }}">
</div>
{% endfor %}
{% endif %}
