b2evolution PHP Cross Reference Blogging Systems

Source: /rsc/js/multiupload/quick_upload.js - 140 lines - 4270 bytes - Summary - Text - Print

   1  // Use this file only together with _file_quick_upload.view.php and quickupload.php
   2  onload = function()
   3  {
   4      var xhr = new XMLHttpRequest;
   5      if( !xhr )
   6      {
   7          alert("Your browser does not support XMLHttpRequest technology!");
   8          return;
   9      }
  10      var file_queue = new Array();
  11      var bars = new Array();
  12      var divs = new Array();
  13      var results = new Array();
  14      var uploading = false;
  15  
  16      function size( bytes )
  17      { // simple function to show a friendly size
  18          var i = 0;
  19          while( 1023 < bytes )
  20          {
  21              bytes /= 1024;
  22              ++i;
  23          };
  24          return  i ? bytes.toFixed( 2 ) + ["", " KB", " MB", " GB", " TB"][i] : bytes + " bytes";
  25      };
  26  
  27      /*
  28       * Start uploading the next file, if the queue is not empty
  29       */
  30      function upload_next()
  31      {
  32          if( file_queue.length == 0 )
  33          {
  34              uploading = false;
  35              return;
  36          }
  37          uploading = true;
  38          var curr_file = new Array();
  39          curr_file.push( file_queue.shift() );
  40          var bar = bars.shift();
  41          var div = divs.shift();
  42          var result_span = results.shift();
  43          var filename = uploading_text + " \"" + curr_file[0].name + "\" ...";
  44  
  45          sendMultipleFiles({
  46  
  47              // list of files to upload
  48              files:curr_file,
  49  
  50              upload_url:url,
  51  
  52              max_size: maxsize,
  53  
  54              // clear the container 
  55              onloadstart:function()
  56              {
  57                  div.innerHTML = filename;
  58                  bar.style.width = "0px";
  59              },
  60  
  61              // do something during upload ...
  62              onprogress:function(rpe)
  63              {
  64                  bar.style.width = (((this.sent + rpe.loaded) * 200 / this.total) >> 0) + "px";
  65              },
  66  
  67              // fired when last file has been uploaded
  68              onload:function(rpe, xhr)
  69              {
  70                  var result_code = xhr.responseText.charAt(0);
  71                  if( result_code !== '0' && result_code !== '1' )
  72                  { // Unsuccessful upload
  73                      div.innerHTML += xhr.responseText;
  74                      bar.style.width = "0px";
  75                      upload_next();
  76                  }
  77                  else
  78                  { // Successful upload
  79                      if( result_code == '1' )
  80                      { // file name was changed, show submit button
  81                          submit.setAttribute("type", "submit");
  82                          div.innerHTML += xhr.responseText.substr(1);
  83                      }
  84                      result_span.innerHTML = ok_text;
  85                      bar.style.width = "200px";
  86                      upload_next();
  87                  }
  88              },
  89  
  90              // if something is wrong ... (from native instance or because of size)
  91              onerror:function(){
  92                  var message = size_error.replace( "%1", size(this.file.fileSize) );
  93                  message = message.replace( "%2", size(maxsize) );
  94                  div.innerHTML = message;//sprintf( size_error, size(this.file.fileSize), size(maxsize) );//"The file " + this.file.fileName + " is too big [" + size(this.file.fileSize) + "]";
  95                  upload_next();
  96              }
  97          });
  98      }
  99  
 100      var input = document.getElementById( "quickupload" );
 101      var upload_queue = document.getElementById( "upload_queue" );
 102      var submit = document.getElementById( "saveBtn" );
 103  
 104      // auto upload on files change
 105      //input.addEventListener("change", function(){
 106      input.onchange = function() {
 107  
 108          var i = 0;
 109          if( ! input.files ) {
 110              alert(incompatible_browser);
 111              return;
 112          }
 113  
 114          while( i < input.files.length )
 115          {
 116              // create new upload node
 117              file_queue.push(input.files[i]);
 118              var newupload = upload_queue.appendChild(document.createElement("div"));
 119              newupload.id = "uploadblock";
 120              var div = newupload.appendChild(document.createElement("div"));
 121              var bar = newupload.appendChild(document.createElement("div")).appendChild(document.createElement("span"));
 122              var result_span = newupload.appendChild(document.createElement("span"));
 123              var separator = newupload.appendChild(document.createElement("div"));
 124              separator.id = "upload_separator";
 125              bar.id = "bar"
 126              bar.parentNode.id = "progress";
 127              result_span.id = "result_success";
 128              bars.push(bar);
 129              divs.push(div);
 130              results.push(result_span);
 131              i++;
 132          }
 133          if( !uploading )
 134          {
 135              upload_next();
 136          }
 137          input.value = "";
 138  
 139      };//, false);
 140  };

title

Description

title

Description

title

Description

title

title

Body