0
I’m building an app to upload videos and photos, and I’d like to put a ProgressBar
to show progress for the user, I found some questions to answer (Like this question), but I’m having a little trouble understanding, and so I couldn’t do the Progressbar work.
Here is my Progressbar:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Salvando as informações, Aguarde...</h4>
</div>
<div class="modal-body">
<div class="progress ">
<div id="progressBar" class="progress-bar active progress-bar-primary progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="1" style="width:0%">
<span id="display"></span>
</div>
</div>
</div>
</div>
</div>
And here’s my requisition:
document.getElementById('formItem').onsubmit = function () {
var formdata = new FormData(); //FormData object
//Creating an XMLHttpRequest and sending
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", transferComplete, false);
xhr.addEventListener("error", transferFailed, false);
xhr.open('POST', '/Unidade/Item/Cadastrar');
if (xhr.upload) {
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) {
progressBar.max = e.total;
progressBar.value = e.loaded;
display.innerText = Math.floor((e.loaded / e.total) * 100) + '%';
}
}
xhr.upload.onloadstart = function (e) {
progressBar.value = 0;
display.innerText = '0%';
}
xhr.upload.onloadend = function (e) {
var percentComplete = (e.loaded / e.total) * 100;
$('#progressbar').progressbar("option", "value", percentComplete);
loadBtn.innerHTML = 'Iniciando o Upload';
}
}
xhr.send(formdata);
return false;
}
In my tests it opens the modal and the percentage increases gradually, but the progression does not change, and the 70% is already redirected as if it had already finished the upload, but it is not finished.
I also tried a in a slightly different way, but it didn’t work either didn’t work, that’s the two Listerner
that I have in the code:
function transferComplete(evt) {
SucessoMensagem(' ', 'Cadastrado Com sucesso!')
$('#myModal').hide();
window.location.href = "/Unidade/Item?PaginaAtual=0&TipoItem=" + $("#Tipo").val() + "&CategoriaId=0";
}
function transferFailed(evt) {
ErroMensagem(' ', 'Algo deu errado! Tente novamente,se o erro persistir,entre em contato.')
window.location.href = "/Unidade/Item?PaginaAtual=0&TipoItem=" + $("#Tipo").val() + "&CategoriaId=0";
}
Thank you very much!
Good morning, what library are you using for the
progressBar
? can show us where the variableprogressBar
is being instantiated? normally, for the progress of a Progress bar, you pass a float value between 0 and 1. A suggestion is to first debug the code by placing aconsole.log(e.loaded);
within theif (e.lengthComputable) {
to check if expected values are being received.– mrlew
I am using the Bootstrap bar, the progression variableBar I have instacied var progressBar = Document.getElementById("progressBar"), Another detail and that after the upload completes the progressibar will fills completely at once, but the number in % runs normally.
– William Cézar
Can you post the result of the above suggested debug? in relation to these bootstrap progressBar, its progress is set through width, and you are setting value.
– mrlew
okay a moment...
– William Cézar