From 8ede0f68a110c8b9db8403499639deb25e315cc2 Mon Sep 17 00:00:00 2001 From: brazilets Date: Sat, 30 Mar 2024 22:27:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D0=94=D0=97=20'=D0=A4=D0=BE=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D1=8C'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/uploadForm.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/js/uploadForm.js b/js/uploadForm.js index 0439f2f..536b4be 100644 --- a/js/uploadForm.js +++ b/js/uploadForm.js @@ -210,5 +210,56 @@ imgUploadForm.addEventListener('submit', (evt) => { }); }); +inputFile.addEventListener('change', function () { + const file = this.files[0]; + const reader = new FileReader(); + + reader.onloadend = function () { + imgPreview.src = reader.result; + }; + + if (file) { + reader.readAsDataURL(file); + } else { + imgPreview.src = ''; + } +}); + + +const errorMessageElement = document.createElement('div'); +errorMessageElement.classList.add('error-message'); +errorMessageElement.textContent = 'Произошла ошибка при загрузке фотографии. Пожалуйста, попробуйте еще раз.'; + +errorMessageElement.style.position = 'fixed'; +errorMessageElement.style.top = '0'; +errorMessageElement.style.left = '0'; +errorMessageElement.style.width = '100%'; +errorMessageElement.style.padding = '10px'; +errorMessageElement.style.backgroundColor = 'red'; +errorMessageElement.style.color = 'white'; +errorMessageElement.style.textAlign = 'center'; +errorMessageElement.style.zIndex = '1000'; +errorMessageElement.style.display = 'none'; + +imgUploadForm.addEventListener('submit', (evt) => { + evt.preventDefault(); + + const formData = new FormData(imgUploadForm); + + sendData(formData) + .then(() => { + errorMessageElement.style.display = 'none'; + }) + + .catch(() => { + errorMessageElement.style.display = 'block'; + setTimeout(() => { + errorMessageElement.style.display = 'none'; + }, 3000); + + }); +}); + +document.body.prepend(errorMessageElement); export { imgPreview };