uploader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2014 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
  2. // This program is free software: you can redistribute it and/or modify it under
  3. // the terms of the GNU General Public License as published by the Free Software
  4. // Foundation, either version 3 of the License, or (at your option) any later
  5. // version.
  6. //
  7. // This program is distributed in the hope that it will be useful, but WITHOUT
  8. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. //
  11. // You should have received a copy of the GNU General Public License along with
  12. // this program. If not, see <http://www.gnu.org/licenses/>.
  13. var lastFiles;
  14. function disableFileUpload() {
  15. document.getElementById('fileToUpload').setAttribute('disabled', 'disabled');
  16. return true;
  17. }
  18. function fileSelected() {
  19. var files = document.getElementById('fileToUpload').files;
  20. var totalSize = 0;
  21. for (i = 0; i < files.length; i++)
  22. totalSize += files[i].size;
  23. if (totalSize >= 1024 * 1024 * 100)
  24. alert('Total filesize should not exceed 100 MB');
  25. if (totalSize != 0) {
  26. if (totalSize > 1024 * 1024)
  27. totalSize = (Math.round(totalSize * 100 / (1024 * 1024)) / 100).toString() + 'MB';
  28. else
  29. totalSize = (Math.round(totalSize * 100 / 1024) / 100).toString() + 'KB';
  30. //document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
  31. document.getElementById('fileSize').innerHTML = 'Size: ' + totalSize;
  32. //document.getElementById('fileType').innerHTML = 'Type: ' + file.type;
  33. document.getElementById('progressNumber').innerHTML = '';
  34. lastFiles = null;
  35. }
  36. }
  37. function uploadFile() {
  38. if (lastFiles === undefined) return; // no file selected
  39. if (lastFiles === null) {
  40. var fd = new FormData();
  41. var files = document.getElementById('fileToUpload').files;
  42. for (i = 0; i < files.length; i++) {
  43. var curFile = files[i];
  44. fd.append('fileToUpload' + i, files[i]);
  45. }
  46. fd.append('key', 'justletmeupload'); // CHANGE THIS
  47. fd.append('nameOnly', '1');
  48. var xhr = new XMLHttpRequest();
  49. xhr.upload.addEventListener('progress', uploadProgress, false);
  50. xhr.addEventListener('load', uploadComplete, false);
  51. xhr.addEventListener('error', uploadFailed, false);
  52. xhr.addEventListener('abort', uploadCanceled, false);
  53. xhr.open('POST', 'http://files.YOURDOMAIN.org/cgi-bin/upload.pl'); // CHANGE THIS
  54. xhr.send(fd);
  55. } else
  56. addFile(lastFiles);
  57. }
  58. function addFiles(filenames) {
  59. var el = document.getElementById('aftertext'); // comment
  60. if (el === null)
  61. el = document.getElementById('text'); // edit
  62. if (el != null) {
  63. el.value += '\n\n';
  64. var lines = filenames.split('\n');
  65. for (var i = 0; i < lines.length; i++)
  66. if (lines[i]) {
  67. if (lines[i].substring(0, 6) != 'Error:')
  68. el.value += '[[File:' + lines[i] + ']]\n';
  69. else
  70. alert(evt.target.responseText);
  71. }
  72. el.scrollTop = el.scrollHeight;
  73. el.focus();
  74. }
  75. }
  76. function uploadProgress(evt) {
  77. if (evt.lengthComputable) {
  78. var percentComplete = Math.round(evt.loaded * 100 / evt.total);
  79. document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
  80. } else
  81. document.getElementById('progressNumber').innerHTML = '...';
  82. }
  83. function uploadComplete(evt) {
  84. lastFiles = evt.target.responseText;
  85. document.getElementById('progressNumber').innerHTML = '100%';
  86. addFiles(evt.target.responseText);
  87. document.getElementById('fileToUpload').value = ''; // TODO do that on Save instead?
  88. }
  89. function uploadFailed(evt) {
  90. alert('There was an error attempting to upload the file.');
  91. }
  92. function uploadCanceled(evt) {
  93. alert('The upload has been canceled by the user or the browser dropped the connection.');
  94. }