javascript XMLHttpRequest 206 部分内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15561508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:12:04  来源:igfitidea点击:

XMLHttpRequest 206 Partial Content

javascriptajaxxmlhttprequest

提问by jamesshuang

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 the server similar to how html5 video is handled.

我想从 JavaScript 中的 XMLHttpRequest 对象发出部分内容请求。我正在从服务器加载一个大型二进制文件,我宁愿从服务器流式传输它,类似于 html5 视频的处理方式。

I can use setRequestHeader to set the Range header. The Network inspector in Chrome shows that the Range header is set successfully. However, the Accept-Encoding header is set to "gzip,deflate", and Chrome will not let me set that header (from W3C standards).

我可以使用 setRequestHeader 来设置 Range 标头。Chrome 中的网络检查器显示 Range 标头设置成功。但是,Accept-Encoding 标头设置为“gzip,deflate”,Chrome 不允许我设置该标头(来自 W3C 标准)。

Is there any way to force the server to respond with a 206 partial content from the XMLHttpRequest object only from javascript?

有什么方法可以强制服务器仅从 javascript 响应来自 XMLHttpRequest 对象的 206 部分内容?

采纳答案by jamesshuang

I think I figured out why the 206 request wasn't working. With gzip compression enabled, the range header gets ignored if the outgoing data can be gzipped.

我想我知道为什么 206 请求不起作用了。启用 gzip 压缩后,如果传出数据可以被 gzip 压缩,则范围标头将被忽略。

The file I was requesting was a large binary file, which nginx interpreted as having mimetype application/octet-stream. This is one of the mimetypes that gets gzipped. If I renamed the file to have a .png filetype, the image/png mimetype is not gzipped, and hence the range request works correctly.

我请求的文件是一个大的二进制文件,nginx 将其解释为具有 mimetype application/octet-stream。这是被 gzip 压缩的 mimetype 之一。如果我将文件重命名为 .png 文件类型,则图像/png mimetype 不会被压缩,因此范围请求可以正常工作。

This is also why setting the Accept-Encoding header with curl to identity also allows the range request to work fine. However, I cannot change that header from an XHR.

这也是为什么将带有 curl 的 Accept-Encoding 标头设置为 identity 也允许范围请求正常工作的原因。但是,我无法从 XHR 更改该标题。

Solution: Change the mimetype table on the server!

解决方法:更改服务器上的mimetype表!

回答by kay - SE is evil

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

这个范围请求对我来说很好用: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 测试一下:

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