【问题标题】:jQuery File Upload show time remaining?jQuery File Upload 显示剩余时间?
【发布时间】:2014-12-03 19:06:35
【问题描述】:

您好,我正在使用jQuery File Upload

它工作正常,我正在向用户显示一个进度条,显示上传进度,代码如下:

$('#fileupload').fileupload({
    /* ... */
    progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
            progress + '%'
        );
    }
});

在我的页面上,我目前刚刚包含了 jquery.fileupload.js 文件。在progressall 函数的数据中,我可以看到比特率、总文件大小和当前加载的数据,正如我所说的那样,它们会按预期更新我的进度条。但是在网站上阅读此link 表明我可以获得包括剩余时间在内的额外信息?但是,我一直无法使其正常工作。还有一个 jquery.fileupload-ui.js 文件 - 我尝试在我的 jquery.fileupload.js 之后包含它,但我没有看到剩余时间属性被添加到 progressall 函数中的数据中。我还尝试删除 jquery.fileupload.js 并只包含 jquery.fileupload-ui.js 但是这破坏了我的文件上传并且它不起作用。

我是否可以使用加载的比特率/数据和总大小轻松计算剩余时间,或者是否有人建议我应该从插件中获取这些扩展信息的正确方法?

【问题讨论】:

  • 你确定你有最新版本的插件吗?
  • 纯数学:在开始上传时获取时间戳并将其保存到某个变量中,并在progressall 函数的每个刻度上计算经过的时间和上传的百分比。例如。如果上传开始于 10 秒前并完成 25%,则剩余时间为 10/25*100 - 10 秒。
  • 刚刚意识到 time 属性仅可使用 progress 函数而不是 progressAll 函数-doh

标签: javascript jquery file-upload jquery-file-upload


【解决方案1】:

我发现最简单的解决方案是计算“fileuploadprogress”事件中的时间:

var secondsRemaining = (data.total - data.loaded) * 8 / data.bitrate;

在我的情况下,我只包括 jquery.fileupload.js 而不是 jquery.fileupload-ui.js,所以我更喜欢这个解决方案。

但是,当您包含 jquery.fileupload-ui.js 组件时,您会获得可用的“扩展进度”信息,但我相信这仅适用于“fileuploadprogressall”事件而不适用于“fileuploadprogress”。 https://github.com/blueimp/jQuery-File-Upload/wiki/Extended-progress-information

【讨论】:

    【解决方案2】:

    从技术上讲,使用比特率是可行的,但它似乎是一个瞬时值,因此您的剩余时间将需要大量平滑,以防止它像疯了一样反弹。与其构建一些复杂的加权平均系统,不如只使用花费的时间和当前的进度来估计剩余时间。

    首先,在添加回调中的数据对象上标记您的开始时间:

    input.bind('fileuploadadd', function(e, data) {
      ...
      data.submitted = new Date().getTime();
      data.submit();
    });
    

    那么很容易在进度回调中获得一个美好、平稳的剩余时间:

    input.bind('fileuploadprogress', function(e, data) {
      var progress = data.loaded / data.total;
      var timeSpent = new Date().getTime() - data.submitted;
      var secondsRemaining = Math.round(((timeSpent / progress) - timeSpent) / 1000);
    });
    

    【讨论】:

      【解决方案3】:

      这就是他们为自定义用户界面所做的。 您可以在

      中使用data参数
      $('#fileupload').bind('fileuploadprogress', function (e, data) {
          _renderExtendedProgress(data);
      });
      

      像这样调用 _renderExtendedProgress

      _renderExtendedProgress: function (data) {
          return this._formatBitrate(data.bitrate) + ' | ' +
              this._formatTime(
                  (data.total - data.loaded) * 8 / data.bitrate
              ) + ' | ' +
              this._formatPercentage(
                  data.loaded / data.total
              ) + ' | ' +
              this._formatFileSize(data.loaded) + ' / ' +
              this._formatFileSize(data.total);
      }
      
      _formatFileSize: function (bytes) {
          if (typeof bytes !== 'number') {
              return '';
          }
          if (bytes >= 1000000000) {
              return (bytes / 1000000000).toFixed(2) + ' GB';
          }
          if (bytes >= 1000000) {
              return (bytes / 1000000).toFixed(2) + ' MB';
          }
          return (bytes / 1000).toFixed(2) + ' KB';
      }
      
      _formatBitrate: function (bits) {
          if (typeof bits !== 'number') {
              return '';
          }
          if (bits >= 1000000000) {
              return (bits / 1000000000).toFixed(2) + ' Gbit/s';
          }
          if (bits >= 1000000) {
              return (bits / 1000000).toFixed(2) + ' Mbit/s';
          }
          if (bits >= 1000) {
              return (bits / 1000).toFixed(2) + ' kbit/s';
          }
          return bits.toFixed(2) + ' bit/s';
      }
      
      _formatTime: function (seconds) {
          var date = new Date(seconds * 1000),
              days = Math.floor(seconds / 86400);
          days = days ? days + 'd ' : '';
          return days +
              ('0' + date.getUTCHours()).slice(-2) + ':' +
              ('0' + date.getUTCMinutes()).slice(-2) + ':' +
              ('0' + date.getUTCSeconds()).slice(-2);
      }
      
      _formatPercentage: function (floatValue) {
          return (floatValue * 100).toFixed(2) + ' %';
      }
      

      你可以参考他们的源代码https://github.com/blueimp/jQuery-File-Upload/blob/7d46990486ff08acfc001b6368b09bce6712c2c2/js/jquery.fileupload-ui.js

      【讨论】:

        【解决方案4】:

        以扩展进度信息为例,试试...

        $('#fileupload').bind('fileuploadprogress', function (e, data) {
            // Log the current bitrate for this upload:
            // console.log(data.bitrate);
            console.log(data);
        });
        

        ... 检查通过此数据点报告的数据。然后,您可以访问所需的内容。

        【讨论】:

        • 非常有用,谢谢。讨厌人们如何拒绝有用的答案。
        猜你喜欢
        • 2020-06-27
        • 1970-01-01
        • 2014-08-14
        • 2021-12-29
        • 2014-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多