Nodejs - HTTP 范围支持/部分文件下载
我正在创建一个音乐网络应用程序,用于流式传输我存储在 MongoDB(GridFS) 中的 MP3。
我的问题:如何添加 http 范围支持,以便我可以开始流式传输音频文件 1/2,而无需等待缓冲区。
我知道GridFS支持读取X字节 - X字节,所以基本上我只需要知道如何让nodejs理解它只需要字节X - X。
谢谢!
I am creating a music web app that streams MP3s that I have stored in MongoDB(GridFS).
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.
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
客户端将发送一个
Range
标头,指定绝对开始和结束字节,后跟总文件长度或“*”。示例:
然后,服务器应返回响应代码
206
(部分内容),并且Content-Length
应仅为传输的数据量。如果范围错误,服务器应返回
416
(请求的范围无法满足),其中Content-Range
字段为字节 */* 或者应该忽略范围请求并返回包含整个文件正文的
200
。服务器还必须发送一个
Accept-Ranges
字段,其中包含接受的范围单位的值,在本例中为bytes
。但范围单位可以是您想要的任何自定义范围单位。来源:rfc2616
The client will send a
Range
header specifying the absolute starting and ending bytes followed by total file length or '*'.Examples:
The server should then return a response code
206
(Partial content) and theContent-Length
should be only the amount of data transmitted.In the case the range is wrong, the server should either return
416
(Requested range not satisfiable) with aContent-Range
field ofbytes */*
or should ignore the range request and return a200
with the entire body of the file.The server must also send an
Accept-Ranges
field with the value of the accepted range unit, in this casebytes
. But the range unit can be any custom range unit you want.Source: rfc2616