小编Cio*_*oby的帖子

使用XMLHttpRequest上传大文件时的进度条

我正在尝试使用XMLHttpRequest和file.slice将一些大文件上传到服务器.
我已经在文档和其他各种链接的帮助下做到了这一点.
由于上传大文件是一项很长的工作,我想为用户提供一个进度条.
经过一些更多的阅读后,我遇到了一个例子,从理论上讲,它完全符合我的需要.
通过获取示例代码并使其适应我的需求,我达成了

var upload =
{
blobs: [],
pageName: '',
bytesPerChunk: 20 * 1024 * 1024,
currentChunk: 0,
loaded: 0,
total: 0,
file: null,
fileName: "",

uploadChunk: function (blob, fileName, fileType) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.responseText) {
                // alert(xhr.responseText);
            }
        }
    };

    xhr.addEventListener("load", function (evt) {
        $("#dvProgressPrcent").html("100%");
        $get('dvProgress').style.width = '100%';
    }, false);

    xhr.addEventListener("progress", function (evt) {
        if (evt.lengthComputable) {
            var progress = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery file-upload xmlhttprequest

14
推荐指数
2
解决办法
5万
查看次数

标签 统计

file-upload ×1

javascript ×1

jquery ×1

xmlhttprequest ×1