Wednesday, 10 May 2017

Downloading .csv file using express and fs.createWriteStream

Is it possible to send a .csv file as response using only fs.createWriteStream?

Example:

app.get('/download', function (req, res) {
    const filename = "test.csv";
    res.attachment(filename);
    const csvStream = fs.createWriteStream(filename);
    //csvStream.write([["a", "b"], ["a1", "b1"], ["a2", "b2"]]);
    res.download(filename)
}

This way I can download an empty "test.csv" file. If I uncomment the write line, I get an error 500. Maybe I'm writing the csv line wrongly?



via Danilo

No comments:

Post a Comment