0

We have an REST API endpoint that will stream multiple GB of data as a response. Currently using xhr and responseType: blob, then In our web interface, we would like to stream that response do a file instead of storing the entire response in memory, and the trying to save it to a file. {oked around the fetch API. Still can't quite figure out how to get something like that to work. What are we missing?

bpeikes
  • 3,495
  • 9
  • 42
  • 80

1 Answers1

0

The canonical HTTP way of doing this is to use a HTTP chunked response. That means that instead of using XHR one points the browser at the file URI and the server should respond with a response that indicates it supports chunking.

You're essentially trying to reinvent the wheel (of downloading files) with XHR and JavaScript.

Point the browser at the proper URI and it will be streamed directly to client's disk. Supports resuming broken downloads out-of-the-box!

loa_in_
  • 1,030
  • 9
  • 20
  • It is a chunked repsonse. The issue is that I believe the XHR pulls the entire stream into memory first. – bpeikes Aug 06 '21 at 18:49
  • It's true. It does. Otherwise the variable containing the response would be indeterminate. – loa_in_ Aug 06 '21 at 23:51
  • See the question linked by @Wyck - the answer here https://stackoverflow.com/a/49917066/1654381 initiates the download of a file. It's the proper way of streaming a web resource to client's disk. – loa_in_ Aug 06 '21 at 23:53
  • The JavaScript in browser CANNOT write a piece of a file/append on user's disk. There's no API for that. – loa_in_ Aug 06 '21 at 23:54
  • What if you need authentication via headers? – bpeikes Aug 07 '21 at 00:39
  • I would... set cookies representing authentication data, point the browser at another (your) endpoint, and that endpoint, server-side, can forward it with any headers you wish to your REST API endpoint. – loa_in_ Aug 08 '21 at 07:32