Sunny
Sunny

Reputation: 478

Saving response as a attachments in Nodejs

In one of my projects, I am hitting external API which giving response as "application/zip" and I need to send the same response to the client so the response can be saved into a file and then the user unzip it and use it. The same scenario is working as expected from postman however from node code the file is generated as corrupted. Following is a code snippet from my cntroller where I am pulling zip data from the API

Service.getZipFileContent(userData, key).then(response=> {
            res.setHeader("content-Type", "application/zip; charset=utf-8");
            res.setHeader("content-Length", response.length);
            res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
            res.send(response);
  });

The response of the API tried from postman is as follows enter image description here

I am not able to correctly send and create the zip file.

Upvotes: 1

Views: 466

Answers (3)

Sachin  Mesare
Sachin Mesare

Reputation: 175

Try to pipe the request with the response that you are getting from the method.

Upvotes: 0

pushti
pushti

Reputation: 66

Try using different charset and result length may be wrong i think, I am not getting where it comes from. you can also write file on local instance and send path to the client.

Upvotes: 2

Vipin Meravi
Vipin Meravi

Reputation: 106

On above snippet, change the content-length from result.length to response.length

Upvotes: 0

Related Questions