reset file input after incorrect image submission

This commit is contained in:
csd4ni3l
2025-08-08 18:33:17 +02:00
parent d6d2508fba
commit a8d47b35e0
2 changed files with 10 additions and 7 deletions

View File

@@ -44,7 +44,7 @@ def register():
elif request.method == "POST": elif request.method == "POST":
username, password = request.form.get("username"), request.form.get("password") username, password = request.form.get("username"), request.form.get("password")
return "a" return f"Username: {username}\nPassword: {password}"
@app.route("/upload", methods=["POST"]) @app.route("/upload", methods=["POST"])
def upload(): def upload():

View File

@@ -42,6 +42,13 @@ file_input.addEventListener("change", function () {
document.getElementById("grass-touching-form").innerHTML += `<img class="preview-img" src="${image_url}"></img>`; document.getElementById("grass-touching-form").innerHTML += `<img class="preview-img" src="${image_url}"></img>`;
} }
function upload_error(error_message) {
console.error(error_message)
file_input.value = ''
document.getElementById("submit").innerHTML = "Submit"
document.getElementById("grass-touching-form").innerHTML += `<div class="text-danger">${error_message}</div>`;
}
function upload_file(file_type, file_content) { function upload_file(file_type, file_content) {
fetch('{{ url_for("upload") }}', { fetch('{{ url_for("upload") }}', {
method: "POST", method: "POST",
@@ -54,9 +61,7 @@ file_input.addEventListener("change", function () {
if (response.status == 401) { if (response.status == 401) {
response.text().then(text => { response.text().then(text => {
if (text) { if (text) {
console.error(text); upload_error(text);
document.getElementById("grass-touching-form").innerHTML += `<div class="text-danger">${text}</div>`;
document.getElementById("submit").innerHTML = "Submit"
} }
} }
@@ -64,9 +69,7 @@ file_input.addEventListener("change", function () {
return; return;
} }
else if (response.status == 400) { else if (response.status == 400) {
document.getElementById("grass-touching-form").innerHTML += `<div class="text-danger">Unknown error happened. Please try again.</div>`; upload_error("Unknown error");
document.getElementById("submit").innerHTML = "Submit"
console.error("Unknown error")
return; return;
} }