I upload a files in my website I have this javascript code integrated to show the progress of upload But the issue is that this bar has value of 100% before the upload is completed How can i resolve this
function uploadFile() {
const fileInput = document.getElementById('file1');
const file = fileInput.files\[0\];
const xhr = new XMLHttpRequest();
xhr.open('POST', 'index.php', true);
xhr.upload.onprogress = function (event) {
if (event.lengthComputable) {
const percentComplete = (event.loaded / event.total) \* 100;
const progress = document.getElementById('progress');
progress.style.width = percentComplete + '%';
progress.textContent = Math.round(percentComplete) + '%';
}
};
xhr.onload = function () {
if (xhr.status === 200) {
alert('File uploaded successfully.');
} else {
alert('Error uploading file.');
}
};
const formData = new FormData();
formData.append('file1', file);
xhr.send(formData);
}