本地主机上的块上传速度更快

时间:2015-04-16 04:12:04

标签: javascript google-chrome upload xmlhttprequest

我正在使用我在另一篇文章中找到的块上传js脚本。当我在本地主机上运行它时,速度很快。但是当我在我的实时网址上运行它时,Chrome网络面板下的请求发送时间大约为20秒。 Request Sent未在localhost上运行。我担心它会在第一个块中发送完整的文件,而不是仅仅1 MB。

上传是否有不同的安全措施,例如运行病毒扫描的Chrome?我有什么方法可以确认吗?

const BYTES_PER_CHUNK = 1024 * 1024; // 1MB chunk sizes.
var slices;
var slices2;

function sendRequest() {
    var xhr;
    var blob = document.getElementById('fileToUpload').files[0];
    var start = 0;
    var end;
    var index = 0;
    responseCount=0;
    // calculate the number of slices we will need
    slices = Math.ceil(blob.size / BYTES_PER_CHUNK);
    slices2 = slices;

    while(start < blob.size) {
        end = start + BYTES_PER_CHUNK;
        if(end > blob.size) {
            end = blob.size;
        }

        uploadFile(blob, index, start, end);

        start = end;
        index++;
    }
}

function uploadFile(blob, index, start, end) {
    var xhr;
    var end;
    var fd;
    var chunk;
    var url;

    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.responseText) {
                responseCount++;
                jQuery('#uploadMessage').text("Uploading Progress: "+Math.ceil((responseCount/slices2)*100)+"%");                
            }

            slices--;

            // if we have finished all slices
            if(slices == 0) {
                mergeFile(blob);
            }
        }
    };

    if (blob.webkitSlice) {
        chunk = blob.webkitSlice(start, end);
    } else if (blob.mozSlice) {
        chunk = blob.mozSlice(start, end);
    }
    chunk = blob.slice(start, end);

    fd = new FormData();
    fd.append("file", chunk);
    fd.append("name", blob.name);
    fd.append("index", index);

    xhr.open("POST", "/index.php?chunkupload=upload", true);
    xhr.send(fd);
}

function mergeFile(blob) {
    jQuery('#uploadMessage').text("Closing Upload");
    var xhr;
    var fd;

    xhr = new XMLHttpRequest();

    fd = new FormData();
    fd.append("name", blob.name);
    fd.append("index", slices2);
    fd.append("userinputname",jQuery('#userinputname').val());
    fd.append("userinputdescr",jQuery('#userinputdescr').val());

    xhr.open("POST", "/index.php?chunkmerge=upload", true);
    xhr.send(fd);
    jQuery('#uploadMessage').text("Upload Complete. Video will be published upon site approval.");
}

0 个答案:

没有答案