文件上传和进度条

时间:2013-01-21 14:01:06

标签: java javascript web dojo

我想根据上传文件时读取的文件数量显示进度条。

我们如何使用JS和Servlet来实现这一点我知道上传我可以使用apache commons lib但是如何根据读取的数据显示progess bar。

我应该根据读取的数据量发送对JS代码的良好响应,如果是这样的话,如何实现?或者任何人都知道更好的选择?

1 个答案:

答案 0 :(得分:0)

我在我的应用程序中使用此代码动态地向表中添加进度条,我确实剪切了一堆代码,因此可能会丢失一些东西。您只需使用post方法将set_percantage调用到服务器即可。

很抱歉,如果还有一些德语写在那里,或者那里有一两个国家,原来这就像是400行,填满了一张大桌子。

编辑:我正在通过applet获取信息,您只需在服务器上写post

function addDownload(filename, datPfad) { 


  var table       = document.getElementById("download_table");
  var body        = table.getElementsByTagName("tbody")[0];

  var progres     = function() {

  var getVars                     = this;    
  this.file                       = filename;

  this.tr                         = document.createElement("tr");

  this.td3                        = document.createElement("td");

  this.progress                   = document.createElement('div');
  this.container                  = document.createElement("div");
  this.progressbar                = document.createElement("div");
  this.bar                        = document.createElement("div");
  this.text                       = document.createElement("div");
  this.path                       = datPfad;

  this.progressbar.style.width     = "100%";
  this.progressbar.style.height    = "20px";
  this.text.style.fontSize         = "10px";

  this.bar.className               = "progress";

  this.progressbar.appendChild(this.bar);


  this.text.style.marginTop        = "-19px"
  this.text.style.textAlign        = "center";
  this.text.style.textSize         = "15px";


  this.text.appendChild(document.createTextNode("Download starting"));
  this.progressbar.appendChild(this.text);

  this.container.appendChild(this.progressbar);

  this.td3.appendChild(this.container);

  this.td3.setAttribute("width", "32%");

  this.tr.appendChild(this.td3);

  this.tr.setAttribute("style", "background:rgb(245, 245, 245)");

  this.tr.setAttribute("id",filename);

  body.appendChild(this.tr);

  this.set_percantage = function() {
          progress                = document.jsap.getProgress(getVars.file);
          this.bar.style.width    = progress + "%";
          downloaded              = document.jsap.getDownloadedSize(getVars.file);
          filesize                = document.jsap.getDownloadSize(getVars.file);
          this.text.removeChild(this.text.firstChild);
          textnode = (downloaded == 0.0 || filesize == -1.0) ? "Download starting" : downloaded + " MB of " + filesize + " MB downloaded";
          this.text.appendChild(document.createTextNode(textnode));
          if(downloaded >= filesize && downloaded != 0) {
              row.beendet();
          }


  }

  this.interval = window.setInterval(function() {
      if(boxbuffer.innerHTML == "finished") {
          window.clearInterval(this.interval);
      }
      row.set_percantage();
  },2000);

}

我在这里忘记了css课程:

.progress {
            width:0%;
            height:100%;
           background: rgb(30,205,230); /* Old browsers */
            /* IE9 SVG, needs conditional override of 'filter' to 'none' */
            background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMTAwJSI+CiAgICA8c3RvcCBvZmZzZXQ9IjAlIiBzdG9wLWNvbG9yPSIjMWVjZGU2IiBzdG9wLW9wYWNpdHk9IjEiLz4KICAgIDxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iIzVhOWNmYSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
            background: -moz-linear-gradient(38deg, rgba(30,205,230,1) 0%, rgba(90,156,250,1) 100%); /* FF3.6+ */
            background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,rgba(30,205,230,1)), color-stop(100%,rgba(90,156,250,1))); /* Chrome,Safari4+ */
            background: -webkit-linear-gradient(38deg, rgba(30,205,230,1) 0%,rgba(90,156,250,1) 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(38deg, rgba(30,205,230,1) 0%,rgba(90,156,250,1) 100%); /* Opera 11.10+ */
            background: -ms-linear-gradient(38deg, rgba(30,205,230,1) 0%,rgba(90,156,250,1) 100%); /* IE10+ */
            background: linear-gradient(38deg, rgba(30,205,230,1) 0%,rgba(90,156,250,1) 100%); /* W3C */
            filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1ecde6', endColorstr='#5a9cfa',GradientType=1 ); /* IE6-8 fallback on horizontal gradient */
            z-index:2;
            }

所以,我基本上做的是,我创建了一个宽度为0%的div元素。 xml返回下载文件的过程。根据这个数字,我增加了div元素的宽度。