Working single file upload
This commit is contained in:
parent
d406574707
commit
d332aca22e
@ -7,6 +7,13 @@
|
|||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
<link rel="stylesheet" type="text/css" href="../static/style.css">
|
<link rel="stylesheet" type="text/css" href="../static/style.css">
|
||||||
<script src="../static/script.js"></script>
|
<script src="../static/script.js"></script>
|
||||||
|
<script>
|
||||||
|
function prepUpload() {
|
||||||
|
uploadFiles('submit', 'fileToUpload', 'progressContainer', 'progress', '/upload/photo', {
|
||||||
|
'name': document.getElementById('buttonName').value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@ -15,7 +22,11 @@
|
|||||||
<div class="alert alert-info text-center" role="alert">
|
<div class="alert alert-info text-center" role="alert">
|
||||||
Upload a single static photo, single photos are good if you want something that doesn't distract during services.
|
Upload a single static photo, single photos are good if you want something that doesn't distract during services.
|
||||||
</div>
|
</div>
|
||||||
<form method="POST" enctype="multipart/form-data" action="/upload/photo">
|
<form>
|
||||||
|
<div id="progressContainer" class="progress" role="progressbar", aria-label="Upload Progress" aria-valuemin="0"
|
||||||
|
aria-valuemin="0", aria-valuemax="100" >
|
||||||
|
<div id="progress" class="progress-bar" style="width: 0%;">0%</div>
|
||||||
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="fileToUpload">File to Upload</label>
|
<label for="fileToUpload">File to Upload</label>
|
||||||
<input type="file" class="form-control" name="file" id="fileToUpload" />
|
<input type="file" class="form-control" name="file" id="fileToUpload" />
|
||||||
@ -24,7 +35,7 @@
|
|||||||
<label for="buttonName">Button Name</label>
|
<label for="buttonName">Button Name</label>
|
||||||
<input type="text" class="form-control" name="buttonName" id="buttonName" />
|
<input type="text" class="form-control" name="buttonName" id="buttonName" />
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-primary form-control">Submit</button>
|
<button id="submit" class="btn btn-primary form-control" onclick="prepUpload()">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@ -1,11 +1,40 @@
|
|||||||
from flask import Blueprint, request, render_template
|
from flask import Blueprint, request, render_template, jsonify, current_app
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
import os
|
||||||
|
|
||||||
upload_photo_bp = Blueprint('upload_photo', __name__, url_prefix='/upload', template_folder='.')
|
upload_photo_bp = Blueprint('upload_photo', __name__, url_prefix='/upload', template_folder='.')
|
||||||
|
|
||||||
@upload_photo_bp.route('/photo')
|
@upload_photo_bp.route('/photo', methods=['GET', 'POST'])
|
||||||
def get_page():
|
def get_page():
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
return render_get(), 200
|
return render_get(), 200
|
||||||
|
elif request.method == "POST":
|
||||||
|
return upload()
|
||||||
|
|
||||||
def render_get():
|
def render_get():
|
||||||
return render_template('upload_photo.html')
|
return render_template('upload_photo.html')
|
||||||
|
|
||||||
|
def upload():
|
||||||
|
if not request.files['files[]']:
|
||||||
|
return jsonify({
|
||||||
|
'status': 'FAILED',
|
||||||
|
'error': '/upload/photo requires 1 photo'
|
||||||
|
}), 400
|
||||||
|
|
||||||
|
# TODO Button name data exists
|
||||||
|
# TODO Button name already exists validation
|
||||||
|
# TODO File input validation
|
||||||
|
print(request.files['files[]'])
|
||||||
|
|
||||||
|
file = request.files['files[]']
|
||||||
|
|
||||||
|
if file:
|
||||||
|
folder_path = os.path.join(current_app.config["config"]["upload_location"], request.form["name"])
|
||||||
|
if not os.path.exists(folder_path):
|
||||||
|
os.makedirs(folder_path)
|
||||||
|
|
||||||
|
filename = secure_filename(file.filename)
|
||||||
|
file.save(os.path.join(folder_path, filename))
|
||||||
|
return jsonify({
|
||||||
|
'status': 'SUCCESS'
|
||||||
|
}), 200
|
||||||
@ -2,5 +2,6 @@
|
|||||||
"database": {
|
"database": {
|
||||||
"type": "SQLite",
|
"type": "SQLite",
|
||||||
"name": "scdb.db"
|
"name": "scdb.db"
|
||||||
}
|
},
|
||||||
|
"upload_location": "uploads"
|
||||||
}
|
}
|
||||||
@ -1,8 +1,17 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
from blueprints.home.home import home_bp
|
from blueprints.home.home import home_bp
|
||||||
from blueprints.upload_photo.upload_photo import upload_photo_bp
|
from blueprints.upload_photo.upload_photo import upload_photo_bp
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
|
with open("config.json", 'r') as config_file:
|
||||||
|
config_json = json.loads(config_file.read())
|
||||||
|
|
||||||
|
if not os.path.exists(config_json["upload_location"]):
|
||||||
|
os.makedirs(config_json["upload_location"])
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.config["config"] = config_json
|
||||||
app.register_blueprint(home_bp)
|
app.register_blueprint(home_bp)
|
||||||
app.register_blueprint(upload_photo_bp)
|
app.register_blueprint(upload_photo_bp)
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,16 @@
|
|||||||
function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint, otherData) {
|
function updatePercentage(input_percent, progressBarContainerID, progressBarID) {
|
||||||
|
const clampedPercentage = Math.min(100, Math.max(0, input_percent))
|
||||||
|
|
||||||
|
const progressBar = document.getElementById(progressBarID)
|
||||||
|
const progressContainer = document.getElementById(progressBarContainerID)
|
||||||
|
|
||||||
|
progressBar.style.width = `${clampedPercentage}%`
|
||||||
|
progressContainer.setAttribute('aria-valuenow', clampedPercentage)
|
||||||
|
|
||||||
|
progressBar.textContent = `${clampedPercentage}%`
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFiles(submitButtonID, fileInputID, progressBarContainerID, progressBarID, targetEndpoint, otherData) {
|
||||||
const submitButton = document.getElementById(submitButtonID)
|
const submitButton = document.getElementById(submitButtonID)
|
||||||
submitButton.disabled = true
|
submitButton.disabled = true
|
||||||
|
|
||||||
@ -33,7 +45,8 @@ function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint,
|
|||||||
xhr.upload.onprogress = function(event) {
|
xhr.upload.onprogress = function(event) {
|
||||||
if(event.lengthComputable) {
|
if(event.lengthComputable) {
|
||||||
const percentComplete = (event.loaded / event.total) * 100
|
const percentComplete = (event.loaded / event.total) * 100
|
||||||
document.getElementById(progressBarID).value = percentComplete
|
|
||||||
|
updatePercentage(percentComplete, progressBarContainerID, progressBarID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xhr.onerror = function() {
|
xhr.onerror = function() {
|
||||||
|
|||||||
0
utilities/__init__.py
Normal file
0
utilities/__init__.py
Normal file
@ -1,21 +1,12 @@
|
|||||||
from flask import g
|
from flask import g, current_app
|
||||||
from database.sqlite import SQLite
|
from .database.sqlite import SQLite
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def get_config():
|
|
||||||
config = getattr(g, "_config", None)
|
|
||||||
|
|
||||||
if config is None:
|
|
||||||
with open("../config.json") as config_file:
|
|
||||||
config = g._config = json.loads(config_file.read())
|
|
||||||
|
|
||||||
return config
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = getattr(g, "_database", None)
|
db = getattr(g, "_database", None)
|
||||||
|
|
||||||
if db is None:
|
if db is None:
|
||||||
if str(get_config()["database"]["type"]).casefold() == "SQLite".casefold():
|
if str(current_app.config["config"]["database"]["type"]).casefold() == "SQLite".casefold():
|
||||||
db = g._database = SQLite(get_config()["database"]["name"])
|
db = g._database = SQLite(current_app.config["config"]["database"]["name"])
|
||||||
|
|
||||||
return db
|
return db
|
||||||
Loading…
Reference in New Issue
Block a user