'res.download() callback function doesn't work properly
The callback function for res.download()
is supposed to run when the download is finished. But sometimes it runs when download is still in progress. Is it a bug? or should I even rely on this callback function to know when a download is completed?
Solution 1:[1]
I think that what you observed is the following:
- The video player makes a first request for your video file, with header
Range: bytes=0-
. - Your server starts streaming, and the video player starts playing. It receives data from your server faster than it needs for playing (otherwise, the video would freeze).
- If you move the seek bar to a part of the video that the player has already received, it skips to there, without any impact on the ongoing request to your server, which still continues streaming.
- But if you move the seek bar to a part of the video that the player has not yet received, it aborts the ongoing request and makes a new request, with header
Range: bytes=1000000-
or so. - The abortion of the first request causes the callback function to be invoked with the "Request abort" error as argument.
- The callback of the second request is not triggered yet. Rather, the first callback is made while the second streaming is still in progress. That's what you observe, but it's entirely OK.
Your callback function should treat such errors specially:
res.download(filePath, fileName, function(err) {
if (err) console.error(err);
else console.log("Download completed");
});
Solution 2:[2]
You have to use async and await for upper function like generatePdf functions and all.
You can got your answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Heiko Theißen |
Solution 2 | Aakash L. |