XMLHttpRequest 206 Partial Content

后端 未结 2 696
Happy的楠姐
Happy的楠姐 2020-11-30 06:50

I would like to issue a partial content request from an XMLHttpRequest object in javascript. I\'m loading a large binary file from the server, and I\'d rather stream it from

2条回答
  •  天涯浪人
    2020-11-30 07:27

    This range-request works fine for me: http://jsfiddle.net/QFdU4/

    var xhr = new XMLHttpRequest;
    
    xhr.onreadystatechange = function () {
      if (xhr.readyState != 4) {
        return;
      }
      alert(xhr.status);
    };
    
    xhr.open('GET', 'http://fiddle.jshell.net/img/logo.png', true);
    xhr.setRequestHeader('Range', 'bytes=100-200'); // the bytes (incl.) you request
    xhr.send(null);
    

    You have to make sure that the server allows range requests, though. You can test it with curl:

    $ curl -v -r 100-200 http://example.com/movie.mkv > /dev/null
    

提交回复
热议问题