开发者

Big file upload causes browser to hang

开发者 https://www.devze.com 2023-04-08 12:27 出处:网络
Small files go smoothly, even when uploading 100 5 Megabyte files at the same time (though it does only handle 5 at a time, queues the rest) but a 150MB file causes the browser to hang several seconds

Small files go smoothly, even when uploading 100 5 Megabyte files at the same time (though it does only handle 5 at a time, queues the rest) but a 150MB file causes the browser to hang several seconds while it initiates.

  function start(file) {
    var xhr = new XMLHttpRequest();
    ++count;

    var container = document.createElement("tr");

    var line = document.createElement("td");
    container.appendChild(line);
    line.textContent = count + ".";

开发者_C百科    var filename = document.createElement("td");
    container.appendChild(filename);
    filename.textContent = file.fileName;
    filename.className = "filename";

    initXHREventTarget(xhr.upload, container);

    var tbody = document.getElementById('tbody');
    tbody.appendChild(container);
    tbody.style.display = "";

    var boundary = "xxxxxxxxx";

    xhr.open("POST", "uploader.php");

    xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary); // simulate a file MIME POST request.  
    xhr.setRequestHeader("Content-Length", file.size); 

    xhr.onreadystatechange = function() {  
        if (xhr.readyState == 4) {
            if ((xhr.status >= 200 && xhr.status <= 200) || xhr.status == 304) {
                if (xhr.responseText != "") {  
                    alert(xhr.responseText); // display response.  
                }  
            }  
        } 
    }

    var body = "--" + boundary + "\r\n";  
    body += "Content-Disposition: form-data; name='upload'; filename='" + file.fileName + "'\r\n";  
    body += "Content-Type: application/octet-stream\r\n\r\n";   
    body += $.base64Encode(file.getAsBinary()) + "\r\n";  
    body += "--" + boundary + "--";
    xhr.sendAsBinary(body);

  }


It IS going to take a non-trivial amount of time to slurp in the file's contents, base64 encode it, and then do your string concatenation. In other words: Behaving as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消