Node.js-HTTP Range支持/部分文件下载 [英] Nodejs - HTTP Range support / Partial file download
问题描述
我正在创建一个音乐Web应用程序,该应用程序可以流式传输存储在MongoDB(GridFS)中的MP3.
I am creating a music web app that streams MP3s that I have stored in MongoDB(GridFS).
我的问题:如何添加http范围支持,以便无需等待缓冲区就可以开始流式传输音频文件的1/2.
My Question: How can I add http range support so that I can start streaming the audio file 1/2 of the way through without having to wait for the buffer.
我知道GridFS支持读取X字节-X字节,因此基本上我只需要知道如何获取Node.js即可理解它只需要X-X字节.
I know GridFS supports reading for X bytes - X bytes, so basically I just need to know how to get nodejs to understand it only needs bytes X - X.
谢谢!
推荐答案
客户端将发送Range
标头,指定绝对的开始和结束字节,后跟文件总长度或'*'.
The client will send a Range
header specifying the absolute starting and ending bytes followed by total file length or '*'.
. The first 500 bytes:
bytes 0-499/1234
. The second 500 bytes:
bytes 500-999/1234
. All except for the first 500 bytes:
bytes 500-1233/1234
. The last 500 bytes:
bytes 734-1233/1234
然后,服务器应返回一个响应代码206
(部分内容),而Content-Length
应该只是传输的数据量.
The server should then return a response code 206
(Partial content) and the Content-Length
should be only the amount of data transmitted.
在范围错误的情况下,服务器应使用bytes */*
的Content-Range
字段返回416
(请求的范围无法满足),或者应忽略范围请求,并返回整个范围的200
文件的正文.
In the case the range is wrong, the server should either return 416
(Requested range not satisfiable) with a Content-Range
field of bytes */*
or should ignore the range request and return a 200
with the entire body of the file.
服务器还必须发送一个Accept-Ranges
字段,其值为接受的范围单位,在这种情况下为bytes
.但是范围单位可以是您想要的任何自定义范围单位.
The server must also send an Accept-Ranges
field with the value of the accepted range unit, in this case bytes
. But the range unit can be any custom range unit you want.
来源: rfc2616
这篇关于Node.js-HTTP Range支持/部分文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!