0

I want to serve large files (>1GB) over HTTPS. I am trying to use the range headers for this so that when the client (browser) requests the range of bytes, it will be served by the server. I have also put a maximum limit on the range i.e. 1MB.

The issue is browsers (tested on Chrome, Firefox) does not initiate range request. The code looks something like this:

@app.get("/download/<file_id>")
def download(file_id):
  ...
  # some logic
  ...
  return file_download_in_ranges(file_location, download=True)

The function file_download_in_ranges checks whether the headers have Content-Range header , if yes, it returns the chunk of data from file in specified range. I have tried to use the same API for HTML5 video player, which worked fine. But, when I create an anchor link with the same API, the browser does not initiate Range request cycle. Do range requests only work for browser inbuilt audio/video players?

Thanks in advance for the suggestions!

Pyag
  • 1
  • 1
  • 1
    Browsers don't issue range requests when they don't need to. There is no reason to send many range requests to download a file which could just as well be downloaded fully by one request. If, however, your download breaks in the middle and you "continue" it through your browser, it very likely issues a range request to just download the missing bytes and not start over again,. – tkausl Sep 21 '21 at 15:55
  • Got it. But what if the size of file is large? My server is running out of memory while loading the whole file. So I want to transfer it in chunks so that my server don't get memory full issues. Is there a way to transfer file using range requests for a single large file? I see you point that browser won't send range requests if not required, but I want it to happen in my case when the user clicks on download link. Is there any way? Or a better approach? – Pyag Sep 25 '21 at 05:29
  • `But what if the size of file is large? My server is running out of memory while loading the whole file.` Then fix your server. There is no reason to load the whole file to memory. – tkausl Sep 26 '21 at 18:38
  • Okay. Thanks for the response. Actually I figured out a way to change the response to transfer encoding chunked. This now does not load whole file in memory, but only few chunks, which solved the problem. – Pyag Sep 29 '21 at 06:41

0 Answers0