From ec41008c83d255eb4ff1ff943afd370d1db2859b Mon Sep 17 00:00:00 2001 From: Dilshad <134768995+ShadBalti@users.noreply.github.com> Date: Wed, 25 Oct 2023 14:48:49 +0500 Subject: [PATCH] Update main.js --- main.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index f337609..9c1e66f 100644 --- a/main.js +++ b/main.js @@ -5,16 +5,26 @@ document.addEventListener('DOMContentLoaded', () => { let fileInput = document.getElementById('file'); let img = document.getElementById('img'); - fileInput.addEventListener('change', function () { - if (this.files && this.files[0]) { - let file = this.files[0]; - reader.onload = function (e) { - img.src = e.target.result; - }; + if (fileInput.files.length > 0) { + let file = fileInput.files[0]; + + reader.onload = e => { + img.src = e.target.result; + }; + + reader.onerror = error => { + console.error('Error reading the file:', error); + }; + + try { reader.readAsDataURL(file); + } catch (err) { + console.error('Error reading the file:', err); } - }); -} + } else { + console.error('No file selected.'); + } +}; }