Files
serwerowe_stefanczyk/Lekcja_04/static/form5.html
T
2026-09-24 10:51:42 +02:00

47 lines
1.6 KiB
HTML

<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zadanie 4</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Upload image or txt file</h1>
<input type="file" name="wyslany_plik" id="wyslany_plik">
<button id="wyslij">upload</button>
<div style="min-width: 100px; min-height: 50px; border: 1px solid black;" id="d1">Tu będzie obraz</div>
<div style="min-width: 100px; min-height: 50px; border: 1px solid black;" id="d2">Tu będzie tekst</div>
<script>
const przycisk = document.getElementById("wyslij")
przycisk.onclick = function () {
const fd = new FormData()
fd.append("wyslany_plik", document.getElementById("wyslany_plik").files[0])
const body = fd
fetch("/process_upload", { method: "post", body })
.then(response => response.json())
.then(function (data) {
console.log(data)
if (data.type.substring(0, 6) == 'image/') {
const div = document.getElementById("d1")
div.innerHTML = `<img src="${'/upload/' + data.path}" alt="Obrazek">`
} else if (data.type == 'text/plain') {
const div = document.getElementById("d2")
div.innerText = data.text
}
})
.catch(err => console.error(err))
}
</script>
</body>
</html>