简体   繁体   English

带有分块请求正文的 XmlHttpRequest?

[英]XmlHttpRequest with chunked request body?

I know how to handle chunked downloads in javascript, using the XmlHttpRequest object.我知道如何使用 XmlHttpRequest 对象在 javascript 中处理分块下载。 Is there any way to perform a chunked upload using javascript, opening a connection but only uploading blobs of data bit by bit?有没有办法使用javascript执行分块上传,打开连接但只一点一点地上传数据块?

I know chunked uploads should be possible with Http 1.1 servers, and have found a lot of references to making chunked uploads using various other platforms (C# java etc.) but have not found any references to doing so in the browser with javascript.我知道使用 Http 1.1 服务器应该可以进行分块上传,并且已经找到了很多关于使用各种其他平台(C# java 等)进行分块上传的参考,但没有找到任何关于在浏览器中使用 javascript 执行此操作的参考。

EDIT: The use case is to stream data up to the server, and not to upload a large file, kind of mirroring the use of a chunked response to stream data down to the client.编辑:用例是将数据向上传输到服务器,而不是上传大文件,有点像使用分块响应将数据向下传输到客户端。 This is as an alternative to making individual ajax requests, since the chunks of data that's going up from client to server are pretty frequent (< 0.5s interval).这是发出单个 ajax 请求的替代方法,因为从客户端到服务器的数据块非常频繁(< 0.5 秒间隔)。

You can use the FileReader API and the slice method. 您可以使用FileReader API和slice方法。

with slice you can get block of data that you can upload, then you need to reassemble them server side. 通过切片,您可以获得可以上传的数据块,然后需要在服务器端重新组装它们。

here is a good intro on how to handle files in javascript http://www.html5rocks.com/en/tutorials/file/dndfiles/ 这是有关如何处理javascript http://www.html5rocks.com/zh-CN/tutorials/file/dndfiles/中文件的一个很好的介绍

you can have a look at http://caniuse.com/#feat=filereader for browser support 您可以查看http://caniuse.com/#feat=filereader以获取浏览器支持

As of today (November 2021), I believe support for UPLOADS using HTTP chunked data transfer is still largely missing in browsers.截至今天(2021 年 11 月),我相信浏览器中仍然缺少对使用 HTTP 分块数据传输的 UPLOADS 的支持。 If you look at the "Send ReadableStream in request body" column of the browser support matrix for the Request (Fetch API):如果您查看请求(Fetch API)的浏览器支持矩阵的“在请求正文中发送可读流”列:
https://developer.mozilla.org/en-US/docs/Web/API/Request#browser_compatibility https://developer.mozilla.org/en-US/docs/Web/API/Request#browser_compatibility
You can see that it is currently "No" for all browsers except "Deno".您可以看到,除“Deno”之外的所有浏览器当前都是“No”。 You will however notice the "Experimental" flag beside the column so it is available experimentally in some browsers such as Chrome.但是,您会注意到该列旁边的“实验性”标志,因此它可以在某些浏览器(例如 Chrome)中进行实验性使用。 I wouldn't hold your breath about it becoming mainstream anytime soon though.不过,我不会对它很快成为主流而屏住呼吸。

HTTP chunked data transfer encoding is not technically necessary for sending data a few chunks at a time, I believe regular HTTP data transfer also only sends data a few chunks at a time but the "chunking" is done at the TCP level instead (please correct me if I'm wrong here). HTTP 分块数据传输编码在技术上不是一次发送几个块数据所必需的,我相信常规 HTTP 数据传输也一次只发送几个块,但“分块”是在 TCP 级别完成的(请更正如果我在这里错了,我)。 Consequently, both protocols can be used to stream a file upload.因此,这两种协议都可用于流式传输文件上传。 WebSockets are of course another option as well. WebSockets 当然也是另一种选择。 The main difference in which protocol to choose is based on whether or not you know the final length of the stream in advance or not.选择哪种协议的主要区别在于您是否提前知道流的最终长度。

If you need to stream upload data for which you DON'T know the length in advance (such as live video, video conference calls, remote desktop sessions, chats, etc.) then your best bet is perhaps the WebSocket API (or something built on top of it):如果您需要流式上传事先不知道长度的数据(例如实时视频、视频电话会议、远程桌面会话、聊天等),那么您最好的选择可能是 WebSocket API(或构建的某些内容)在它的上面):
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

If you need to stream upload data for which you DO know the length in advance (files, images, videos, etc.) then I believe your best bet is probably a normal POST or PUT using the Fetch API:如果您需要流式上传您事先知道长度的数据(文件、图像、视频等),那么我相信您最好的选择可能是使用 Fetch API 的普通 POST 或 PUT:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
or still the old XmlHttpRequest API:或者仍然是旧的 XmlHttpRequest API:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM