Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3249676

Browse files
authored
Add files via upload
1 parent 13033c5 commit 3249676

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

python-flask-ajax-file-upload/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from flask import Flask
2+
3+
UPLOAD_FOLDER = 'C:/uploads'
4+
5+
app = Flask(__name__)
6+
app.secret_key = "secret key"
7+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
8+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

python-flask-ajax-file-upload/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
#import magic
3+
import urllib.request
4+
from app import app
5+
from flask import Flask, flash, request, redirect, render_template, jsonify
6+
from werkzeug.utils import secure_filename
7+
8+
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
9+
10+
def allowed_file(filename):
11+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
12+
13+
@app.route('/')
14+
def upload_form():
15+
return render_template('file-upload.html')
16+
17+
@app.route('/python-flask-files-upload', methods=['POST'])
18+
def upload_file():
19+
# check if the post request has the file part
20+
if 'files[]' not in request.files:
21+
resp = jsonify({'message' : 'No file part in the request'})
22+
resp.status_code = 400
23+
return resp
24+
25+
files = request.files.getlist('files[]')
26+
27+
errors = {}
28+
success = False
29+
30+
for file in files:
31+
if file and allowed_file(file.filename):
32+
filename = secure_filename(file.filename)
33+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
34+
success = True
35+
else:
36+
errors[file.filename] = 'File type is not allowed'
37+
38+
if success and errors:
39+
errors['message'] = 'File(s) successfully uploaded'
40+
resp = jsonify(errors)
41+
resp.status_code = 206
42+
return resp
43+
if success:
44+
resp = jsonify({'message' : 'Files successfully uploaded'})
45+
resp.status_code = 201
46+
return resp
47+
else:
48+
resp = jsonify(errors)
49+
resp.status_code = 400
50+
return resp
51+
52+
if __name__ == "__main__":
53+
app.run()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/ajax-files-upload-using-python-flask-jquery/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Python Flask File(s) Upload Example</title>
5+
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
6+
<script type="text/javascript">
7+
$(document).ready(function (e) {
8+
$('#upload').on('click', function () {
9+
var form_data = new FormData();
10+
var ins = document.getElementById('multiFiles').files.length;
11+
12+
if(ins == 0) {
13+
$('#msg').html('<span style="color:red">Select at least one file</span>');
14+
return;
15+
}
16+
17+
for (var x = 0; x < ins; x++) {
18+
form_data.append("files[]", document.getElementById('multiFiles').files[x]);
19+
}
20+
21+
$.ajax({
22+
url: 'python-flask-files-upload', // point to server-side URL
23+
dataType: 'json', // what to expect back from server
24+
cache: false,
25+
contentType: false,
26+
processData: false,
27+
data: form_data,
28+
type: 'post',
29+
success: function (response) { // display success response
30+
$('#msg').html('');
31+
$.each(response, function (key, data) {
32+
if(key !== 'message') {
33+
$('#msg').append(key + ' -> ' + data + '<br/>');
34+
} else {
35+
$('#msg').append(data + '<br/>');
36+
}
37+
})
38+
},
39+
error: function (response) {
40+
$('#msg').html(response.message); // display error response
41+
}
42+
});
43+
});
44+
});
45+
</script>
46+
</head>
47+
<body>
48+
<h2>Python Flask File(s) Upload - Select file(s) to upload</h2>
49+
<dl>
50+
<p>
51+
<p id="msg"></p>
52+
<input type="file" id="multiFiles" name="files[]" multiple="multiple"/>
53+
<button id="upload">Upload</button>
54+
</p>
55+
</dl>
56+
</body>

0 commit comments

Comments
 (0)