0

我知道这听起来很傻......它应该在那里工作......但是:

function fetch(id){
    var fetch = new XMLHttpRequest();
    fetch.open("GET", "/upload/status?X-Progress-ID=" + id);
    fetch.onreadystatechange = function () {
      console.log(fetch.readyState);
    }
    fetch.send(null);
}

这段代码在 IE 和 firefox 中工作......我返回了所有 4 个状态......但是当我在 chrome 中尝试它时,我什么也没得到......而且我完全不知道为什么......

我想你已经猜到了这个问题,为什么它不起作用?

编辑

稍微更改了代码...正如您可能已经猜到的那样,我使用 nginx 上传进度模块,并且此函数每秒调用一次以获取上传文件的进度...

编辑 1

好吧,我试图重写 $.ajax() 的东西,我基本上得到了相同的结果.. 即 firefox 可以工作,而 chrome 什么也没给我。这是页面的完整代码:

<!doctype html>
<html lang="ru">
<head>
  <meta charset="utf-8">
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
  <script src="static/js/jquery-ui-1.8.21.custom.min.js" type="text/javascript"></script>
  <script>
    function add() {
      if (parseInt(document.getElementById('count').getAttribute('value')) < 8) {
        var input = document.createElement('input');
        input.setAttribute('type','file');
        input.setAttribute('multiple','');
        input.setAttribute('name','file[]');
        document.getElementById('multiple').appendChild(input);
        document.getElementById('multiple').appendChild(document.createElement('br'));
        document.getElementById('count').setAttribute('value',parseInt(document.getElementById('count').getAttribute('value'))+1);
      }
      else {
        alert('Можно загрузить не более 8 файлов за раз.');
      }
    }
    function progress() {
      var ms = new Date().getTime() / 1000;
      rq = 0;
      id = "";
      for (i = 0; i < 32; i++) {
        id += Math.floor(Math.random() * 16).toString(16);
      }
      document.getElementById('upload').action = "/upload/share?X-Progress-ID=" + id;
//      console.log("/upload/share?X-Progress-ID=" + id);
      document.getElementById('status').style.display = 'block'
      interval = window.setInterval(function () { fetch(id, ms); }, 1000);
      return true;
    }
    function fetch(id, ms) {
//      console.log("/upload/status?X-Progress-ID=" + id);


      $.ajax({
        type: "GET",
        url: "/upload/status?X-Progress-ID="+id,
        dataType: 'text',
        complete: function(upload) {
          console.log('test');
          alert('test');
        }
      });

//      var fetch = new XMLHttpRequest();
//      fetch.open("GET", "/upload/status", 1);
//      fetch.setRequestHeader("X-Progress-ID", id);
//      fetch.open("GET", "/upload/status?X-Progress-ID=" + id);
//      fetch.setRequestHeader("X-Progress-ID", id);

//      fetch.onreadystatechange = function () {
//        console.log('rorororo');
//        console.log(fetch.readyState);
//        if (fetch.readyState == 3) {
//          console.log(fetch.responseText);
//
////          if (fetch.status == 200) {
//
//            var now = new Date().getTime() / 1000;
//            var upload = eval(fetch.responseText);
//
//            if (upload.state == 'uploading') {
//              var diff = upload.size - upload.received;
//              var rate = upload.received / upload.size;
//              var elapsed = now - ms;
//              var speed = upload.received - rq; rq = upload.received;
//              var remaining = (upload.size - upload.received) / speed;
//              var uReceived = parseInt(upload.received) + ' bytes';
//              var uDiff = parseInt(diff) + ' bytes';
//              var tTotal = parseInt(elapsed + remaining) + ' secs';
//              var tElapsed = parseInt(elapsed) + ' secs';
//              var tRemaining = parseInt(remaining) + ' secs';
//              var percent = Math.round(100*rate) + '%';
//              var uSpeed = speed + ' bytes/sec';
//              document.getElementById('length').firstChild.nodeValue = parseInt(upload.size) + ' bytes';
//              document.getElementById('sent').firstChild.nodeValue = uReceived;
//              document.getElementById('offset').firstChild.nodeValue = uDiff;
//              document.getElementById('total').firstChild.nodeValue = tTotal;
//              document.getElementById('elapsed').firstChild.nodeValue = tElapsed;
//              document.getElementById('remaining').firstChild.nodeValue = tRemaining;
//              document.getElementById('speed').firstChild.nodeValue = uSpeed;
//              document.getElementById('bar').firstChild.nodeValue = percent;
//              document.getElementById('bar').style.width = percent
//            }
//            else {
//              window.clearTimeout(interval);
//            }
////          }
//        }

//      }
//      fetch.send(null);
    }
  </script>
</head>
<body>
<form method="post" enctype="multipart/form-data" id="upload" onsubmit="progress();">
  <input type="hidden" id="count" value="1" />
  <input type="hidden" value="GOBLEBOELBOE" name="secret">
  <div id="multiple">
    <input type="file" name="file[]" multiple /><br>
  </div>
  <input type="submit">
  <a href="#" onclick="add();">add();</a>
</form>
<div id="status" style="display: none;">
  <table width="100%">
    <tr><th></th><th>загрузка</th><th>осталось</th><th>всего</th></tr>
    <tr><td>время:</td><td id="elapsed">∞&lt;/td><td id="remaining">∞&lt;/td><td id="total">∞&lt;/td></tr>
    <tr><td>размер:</td><td id="sent">0 b</td><td id="offset">0 b</td><td id="length">0 b</td></tr>
    <tr><td>скорость:</td><td id="speed">n/a</td></tr>
  </table>
  <div style="border: 1px solid #c0c0c0;">
    <div style="background: #c0c0c0; width: 0%; text-align: right;" id="bar">0%</div>
  </div>
  <a href="#" onclick="if (confirm('Вы точно хотите отменить загрузку?')) window.location = '/'" id="cancel">cancel_upload();</a>
</div>
</body>
</html>

答案: http ://code.google.com/p/chromium/issues/detail?id=45196

4

2 回答 2

0

你应该添加这个

fetch.send(null)

打开()

初始化一个请求。此方法将在 JavaScript 代码中使用;要从本机代码初始化请求,请改用 openRequest()。

发送()

发送请求。如果请求是异步的(这是默认设置),则此方法会在请求发送后立即返回。如果请求是同步的,则此方法在响应到达之前不会返回。

引用自MDN

于 2012-07-30T12:03:09.913 回答
0

显然,chrome 有这么多的错误。 http://code.google.com/p/chromium/issues/detail?id=45196

于 2012-08-01T22:43:28.557 回答