Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Why am I getting empty file from pipe when tried multiple times?
router.get('/excel', (req, res) => {
    Person.find()
        .then((result) => {
            const writeStream = fs.createWriteStream("file.xls");
            const header = "Name" + "\t" + " Age" + "\n";
            writeStream.write(header);
            for (let i = 0; i < result.length; i++) {
                const row = result[i].name + "\t" + result[i].age + "\n";
                writeStream.write(row);
            }
            writeStream.close();
        }).then(() => {
            res.setHeader('Content-disposition', 'attachment; filename=file.xls');
            var filestream = fs.createReadStream(__dirname + '/../file.xls');
            filestream.pipe(res);
        }).catch((err) => {
            res.send(err);
        });
});

I have this code block to write an xls file and download it from client end. Though it gives the file with content first one or two times, thereafter it gives an empty xls file. Can someone help me out with this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!