$( document ).ready(function() { updateFormUploadComponent(); }); function updateFormUploadComponent(){ // Variable initialisation let nb_max_files = 0, forms_data = [], parent = $('.file-manager-form-upload'); // Element initialization $('.materialboxed').materialbox(); // Upload files 'XHR request' parent.each(function(){ $(this).find("#file-manager-input-upload").change( function() { let publicKey = $(this).attr('data-publicKey'); let upload_url = $(this).attr('data-path'); nb_max_files++; let curent_index = nb_max_files; const xhr = new XMLHttpRequest(); let $_files = $(this)[0]; let nbr_files = $_files.files.length; xhr.open('POST', upload_url); let progress_bar = '
Upload Files

'; $(".notifications").append(progress_bar); forms_data[curent_index] = new FormData(); forms_data[curent_index].append('csrf', parent.find("#file-manager-upload-csrf").val()); forms_data[curent_index].append('publicKey', publicKey); for (let i = 0; i < nbr_files; i++) { forms_data[nb_max_files].append('files[]', $_files.files[i]); } xhr.addEventListener('load', function() { $("#file-manager-content").html(xhr.responseText); //lancer test antivirus // antivirus(); setTimeout(function(){ $("#progress-"+curent_index).fadeOut( "slow", function() { $("#progress-"+curent_index).remove(); }); }, 3000); }); xhr.upload.addEventListener('progress', function(e){ $('#progress-'+curent_index).show(); let loaded = Math.round((e.loaded / e.total) * 100); if (loaded > 100) loaded = 100; if (nbr_files === 1){ $('#progress-'+curent_index+ ' .rate-upload').text($_files['files'][0].name + ': ' + loaded + '%'); }else { $('#progress-'+curent_index+ ' .rate-upload').text(nbr_files + ' item to download' + ': ' + loaded + '%'); } $('#progress-'+curent_index+' .progressbar').css('width', loaded + '%'); }); xhr.send(forms_data[curent_index]); }); }); $('.fixed-action-btn').floatingActionButton(); } window.updateFormUploadComponent = updateFormUploadComponent;