繁体   English   中英

jQuery File Upload显示剩余时间?

[英]jQuery File Upload show time remaining?

嗨,我正在使用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函数的数据中,我可以看到比特率,总文件大小和当前加载的数据,正如我所说的,它们可以按预期更新进度条。 但是,在网站上阅读此链接后 ,它暗示我可以获得更多信息,包括剩余时间? 我一直无法使这个工作。 也有一个jquery.fileupload-ui.js文件-我尝试在jquery.fileupload.js之后添加该文件,但是我没有看到剩余时间被添加到progressall函数中的数据中。 我还尝试删除jquery.fileupload.js,仅包含jquery.fileupload-ui.js,但是这破坏了我的文件上传,并且无法正常工作。

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

我发现最简单的解决方案是计算“ 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

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

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

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);
});

这就是他们为自定义ui所做的工作。 您可以在以下位置使用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/c2/js/jquery.fileupload-ui.js中引用其源代码

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM