Skip to content
Advertisement

Progress for a fetch blob javascript

I’m trying to do a javascript fetch to grab a video file using fetch. I am able to get the file downloaded and get the blob URL, but I can’t seem to get the progress while its downloading.

I tried this:

JavaScript

The problem is that I get “Response.blob: Body has already been consumed”. I see that the reader.read() is probably doing that. How do I just get the amount of data received and then get a blob URL at the end of it?

Thanks.

Advertisement

Answer

Update:

My first attempt collected the chunks as they downloaded and them put them back together, with a large (2-3x the size of the video) memory footprint. Using a ReadableStream has a much lower memory footprint (memory usage hovers around 150MB for a 1.1GB mkv). Code largely adapted from the snippet here with only minimal modifications from me:

https://github.com/AnthumChris/fetch-progress-indicators/blob/master/fetch-basic/supported-browser.js

JavaScript
JavaScript

First Attempt (worse, suitable for smaller files)

My original approach. For a 1.1GB mkv, the memory usage creeps up to 1.3GB while the file is downloading, then spikes to about 3.5Gb when the chunks are being combined. Once the video starts playing, the tab’s memory usage goes back down to ~200MB but Chrome’s overall usage stays over 1GB.

Instead of calling response.blob() to get the blob, you can construct the blob yourself by accumulating each chunk of the video (value). Adapted from the exmaple here: https://javascript.info/fetch-progress#0d0g7tutne

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement