3

This is a stripped version of a file download module.
It's an Electron app with React on the front-end, so all Node related things work.

Besides the below code, i'm using res.body.destroy() to close the download at any point.
Also using node-fetch to manage the download process.

I'm looking for a way to Pause and Resume the download.

Any example or link would be really apreciated.
Thanks!

import fetch from 'node-fetch';
import fs from 'fs';

let receivedBytes = 0;
let onEachProgress = 0;

const res = await fetch(url);

const totalBytes = res.headers.get('content-length');

const fileStream = fs.createWriteStream(`....zip`);

(downloadStart(fileId));

res.body.pipe(fileStream);

res.body.on('data', chunk => {
  const percentage = Math.round((receivedBytes * 100) / totalBytes);
  receivedBytes += chunk.length;
  onEachProgress += 1;

  downloadProgress(res);
});

res.body.on('end', () => {
  dispatch(downloadEnd());
});
Cristian Muscalu
  • 9,007
  • 12
  • 44
  • 76

0 Answers0