java - 文件上传和进度条

标签 java javascript web dojo

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

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

我是否应该根据读取的数据量向 JS 代码发送大量响应,如果可以,如何实现?或者有人知道更好的选择吗?

最佳答案

我在我的应用程序中使用这段代码将进度条动态添加到表格中,我确实剪掉了一堆代码,所以可能会遗漏一些东西。您可以在 set_percantage 方法中简单地调用一个 post 到服务器。

很抱歉,如果那里仍然写着一些德语,或者如果那里有太多的一两个陈述句,那么最初这就像 400 行,填满了一张大 table 。

编辑:我通过 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 元素的宽度。

关于java - 文件上传和进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14440281/

相关文章:

java - Spring Tool Suite (STS) 4.8.0 RELEASE - 从 4.7.2 升级后 Java 消失

java - jvisualvm:卡在 "Loading Heap Dump"屏幕上

javascript - React 中 for 循环内的异步函数

javascript - JSON.parse 返回 [Object object] 寻找访问对象的属性

java - 无法到达服务器时程序关闭

java - 等待 Executor 中的所有线程完成?

javascript - 将 PHP 变量传递回 ajax

javascript - Angular Ionic @ViewChild 失去引用

web - 使用 MediaWiki API 下载图像?

php - MySQL删除记录