Blob数据未通过XHR

时间:2017-03-24 06:52:34

标签: javascript php xmlhttprequest

我正在尝试将文件分块并按如下方式发送到服务器

var fileselect = document.getElementById("file");   
    fileselect.addEventListener("change", FileSelectHandler, false);

  function FileDragHover(e) {
      e.stopPropagation();
      e.preventDefault(); 
  }

  function FileSelectHandler(e) {
      FileDragHover(e);

      var blob = e.target.files[0] || e.dataTransfer.files[0];

      worker = new Worker("fileupload.js");
      worker.postMessage(blob);

      worker.onmessage = function(e) {
          console.log(e);
      };       
  }

fileupload.js

self.onmessage = function(e) {
    const BYTES_PER_CHUNK = 1024 * 1024 * 32;

    var blob = new Blob([e.data]), 
        start = 0,
        index = 0,
        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;

        uploadChunk(blob, index, start, end, slices, slices2);

        start = end;
        index++;
    }
};

function uploadChunk(blob, index, start, end, slices, slices2) {
    var xhr = new XMLHttpRequest();

    xhr.onload = function() {
        slices--;

        if (slices == 0) {
            var xhrMerge = new XMLHttpRequest();            
            xhrMerge.open("POST", "uploadlargefile/?a=merge&name=" + blob.name + "&slices=" + slices2);

            xhrMerge.onload = function() {
                self.close();
            };

            xhrMerge.send();
        }
    };

    xhr.upload.onprogress = function(e) {
        if (e.lengthComputable) self.postMessage(Math.round(100 / e.total * e.loaded)); //this doesn't work o.O
    };

    var chunk = blob.slice(start, end);

    xhr.open("POST", "uploadlargefile/?a=chunk&name=" + blob.name + "&index=" + index); 
    xhr.setRequestHeader("Content-Type", "multipart\/form-data; boundary=--------------------");  
    xhr.send(chunk);
}

PHP

print_r($_GET['name']); print_r($_FILES);die;

我可以获取文件的名称,但不能获取文件。任何可能出错的建议

0 个答案:

没有答案