0

I have tried this code to send response zip in header, but something is missing from my side. In this I am getting response as shown in screenshot

enter image description here

Here is my code..

const zipPath - './test.zip'; // I have a zip with 2 files inside it (password protected)
    const stat = fs.statSync(zipPath);
    res.setHeader('Content-Disposition', 'attachment; filename=devices.zip');
    res.setHeader('Content-Type', 'application/octet-stream');
    res.setHeader('Content-Length', stat.size);
     res.writeHead(200, {
       'Content-Type': 'application/octet-stream',
       'Content-Disposition': 'attachment; filename=test.zip',
       'Content-Length': stat.size
     });

    let fileStream = fs.createReadStream(zipPath);
    fileStream.pipe(res);
    res.end();
Sudharshan Nair
  • 1,152
  • 1
  • 10
  • 24
  • Does this answer your question? [How to return zip file from node js api and handle on client side?](https://stackoverflow.com/questions/54487369/how-to-return-zip-file-from-node-js-api-and-handle-on-client-side) – Smily Dec 11 '19 at 12:13
  • Nope actually, I have tried that – Sudharshan Nair Dec 11 '19 at 12:46

1 Answers1

0

It seems like you are able to send the data but the encoding is not proper. The content-type should be 'application/zip' in your case instead of 'application/octet-stream'.

    const zipPath - './test.zip'; // I have a zip with 2 files inside it (password protected)
    const stat = fs.statSync(zipPath);
    res.setHeader('Content-Disposition', 'attachment; filename=devices.zip');
    res.setHeader('Content-Type', 'application/zip');
    res.setHeader('Content-Length', stat.size);
     res.writeHead(200, {
       'Content-Type': 'application/zip',
       'Content-Disposition': 'attachment; filename=test.zip',
       'Content-Length': stat.size
     });

    let fileStream = fs.createReadStream(zipPath);
    fileStream.pipe(res);
    res.end();
Dipanshu
  • 39
  • 5