简体   繁体   中英

Chunked request using XHR

Is there any way to use XMLHttpRequest to send a chunked request?

The use case is uploading streaming data to a server. I need to be able to upload binary data in smaller blocks, but I would prefer to avoid sending a full HTTP request for each block.

The HTTP 1.1 specification allows for chunked requests, but I have been unable to find any example in Javascript.

Thanks

I don't know of any libraries that implement this off the top of my head, but you can use raw XMLHttpRequest and the progress callbacks. Keep in mind that chunked encoding will trigger browser repaints at uncontrollable intervals that change depending on the browser.

You might want to look at WebSockets , which are designed to handle this scenario in a much more user-friendly API.

Possible.

You can set requestHeader "Content-Type: multipart/form-data; boundary=xxxxxxxxxxxx" , which contains the boundary, and construct a Buffer object with correct data.

Not all browsers support Buffer , so you should test first. In my case,Chrome and Firefox work well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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