You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

  1. function onFilesSelected(e) {
  2. var button = e.srcElement;
  3. button.disabled = true;
  4. var progress = document.querySelector('div#progress');
  5. progress.innerHTML = '0%';
  6. var files = e.target.files;
  7. var totalFiles = files.length;
  8. var filesSent = 0;
  9. if (totalFiles) {
  10. var uploader = new Uploader('ws://localhost:8080', function () {
  11. Array.prototype.slice.call(files, 0).forEach(function(file) {
  12. if (file.name == '.') {
  13. --totalFiles;
  14. return;
  15. }
  16. uploader.sendFile(file, function(error) {
  17. if (error) {
  18. console.log(error);
  19. return;
  20. }
  21. ++filesSent;
  22. progress.innerHTML = ~~(filesSent / totalFiles * 100) + '%';
  23. console.log('Sent: ' + file.name);
  24. });
  25. });
  26. });
  27. }
  28. uploader.ondone = function() {
  29. uploader.close();
  30. progress.innerHTML = '100% done, ' + totalFiles + ' files sent.';
  31. }
  32. }
  33. window.onload = function() {
  34. var importButtons = document.querySelectorAll('[type="file"]');
  35. Array.prototype.slice.call(importButtons, 0).forEach(function(importButton) {
  36. importButton.addEventListener('change', onFilesSelected, false);
  37. });
  38. }